Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
karan0396 committed Sep 14, 2022
1 parent 2cbfb26 commit e7b70d0
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 72 deletions.
39 changes: 21 additions & 18 deletions app/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,31 @@ type UserHandler struct {
}

func (u UserHandler) CreateCollection(w http.ResponseWriter, r *http.Request) {
id:= uuid.New()
var collection *dto.JsonFile
id := uuid.New()
var request dto.CollectionRequest
err := json.NewDecoder(r.Body).Decode(&request)
if err != nil{
writeResponse(w,http.StatusBadRequest,err.Error())
}else{
request.User_id = id.String()
collection,appError := u.service.NewCollection(request)
if appError != nil{
writeResponse(w,appError.Code,appError.Message)
}else{
writeResponse(w,http.StatusCreated,collection)
if err != nil {
writeResponse(w, http.StatusBadRequest, err.Error())
}
request.User_id = id.String()
collection, appError := u.service.NewCollection(request)
if appError != nil {
writeResponse(w, appError.Code, appError.Message)
} else {
response := dto.CollectionResponse{
Id: request.User_id,
Config: collection,
}
writeResponse(w,http.StatusOK,response)
}

}


func writeResponse(w http.ResponseWriter,code int,data interface{}){
w.Header().Add("Content-Type","application/json")
w.WriteHeader(code)
if err:=json.NewEncoder(w).Encode(data);err!=nil{
panic(err)
}
}
func writeResponse(w http.ResponseWriter, code int, data interface{}) {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(code)
if err := json.NewEncoder(w).Encode(data); err != nil {
panic(err)
}
}
9 changes: 6 additions & 3 deletions domain/UserRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ func (d UserRepositoryDb)AddUser(c Collection,add []dto.Address)(*errs.AppError)
result,err := d.client.Exec(sqlInsert,c.User_id,c.Name,c.Symbol,c.Description,c.Total_supply,c.Seller_fee,c.Mint_price,c.Game_resource,c.Live_mint_start)
if err!=nil{
logger.Error("Error While creating new account for collection "+err.Error())
return errs.NewUnexpectedError("Unexpected error from database")
// return errs.NewUnexpectedError("Unexpected error from database")
return nil
}

id,err:=result.LastInsertId()
if err!=nil{
logger.Error("Error While getting last insert id"+err.Error())
return errs.NewUnexpectedError("Unexpected error from database")
// return errs.NewUnexpectedError("Unexpected error from database")
return nil
}

userId := strconv.FormatInt(id,10)
Expand All @@ -40,7 +42,8 @@ func (d UserRepositoryDb)AddUser(c Collection,add []dto.Address)(*errs.AppError)
_,err := d.client.Exec(addInsert,userId,v.Address,v.Share)
if err!=nil{
logger.Error("Error While creating new account"+err.Error())
return errs.NewUnexpectedError("Unexpected error from database")
// return errs.NewUnexpectedError("Unexpected error from database")
return nil
}
}
return nil
Expand Down
74 changes: 32 additions & 42 deletions dto/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dto

import (
"collection/errs"
"fmt"
"time"

"gopkg.in/guregu/null.v4"
Expand All @@ -27,15 +26,8 @@ type Address struct {
}

type CollectionResponse struct {
Name string `json:"name"`
Symbol string `json:"symbol"`
Description string `json:"description"`
Total_supply uint32 `json:"total_supply"`
Seller []Address `json:"seller"`
Seller_fee float32 `json:"seller_fee"`
Mint_price float32 `json:"mint_price"`
Game_resource string `json:"game_resource"`
Live_mint_start string `json:"live_mint_start"`
Id string
Config *JsonFile
}

type JsonFile struct {
Expand All @@ -60,55 +52,53 @@ type JsonFile struct {
ShdwStorageAccount null.String
}

type Config struct{
Name string
Symbol string
Description string
type Config struct {
Name string
Symbol string
Description string
Seller_fee_basis_points uint32
Image string
Animation_url string
Attribute []Attribute
Properties Properties

Image string
Animation_url string
Attribute []Attribute
Properties Properties
}

type Attribute struct{
type Attribute struct {
Trait_type string
Value string
Value string
}

type Properties struct{
File File
type Properties struct {
File File
Category string
Creators []Address
}

type File struct{
Uri string
type File struct {
Uri string
Type string
}

func (c CollectionRequest)ToValidate()*errs.AppError{
if (c.Name == ""||c.Description == "" || c.Game_resource == "" || c.Live_mint_start == "" || c.Symbol== "" || c.Total_supply == 0 || c.Seller == nil || c.Seller_fee == 0 || c.Mint_price == 0){
func (c CollectionRequest) ToValidate() *errs.AppError {
if c.Name == "" || c.Description == "" || c.Game_resource == "" || c.Live_mint_start == "" || c.Symbol == "" || c.Total_supply == 0 || c.Seller == nil || c.Seller_fee == 0 || c.Mint_price == 0 {
return errs.NewValidationError("Fields Empty")
}
if c.Mint_price < 0{
if c.Mint_price < 0 {
return errs.NewValidationError("Price should be Greater than Zero")
}
var share int
var share int
var index int
for i,v := range c.Seller{
for i, v := range c.Seller {
share += v.Share
index += i
}

if index > 4 {
return errs.NewValidationError("address should not be more than 4.")
}
fmt.Println(share)
if (share > 100 || share < 100){
return errs.NewValidationError("Share should be 100")
}
return nil

}
}

if index > 4 {
return errs.NewValidationError("address should not be more than 4.")
}
if share > 100 || share < 100 {
return errs.NewValidationError("Share should be 100")
}
return nil

}
23 changes: 23 additions & 0 deletions log.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,26 @@
{"level":"info","timestamp":"2022-09-14T19:03:25.903+0530","caller":"logger/logger.go:38","msg":"Database is Connected"}
{"level":"error","timestamp":"2022-09-14T19:03:28.992+0530","caller":"logger/logger.go:46","msg":"Error While creating new account for collection dial tcp 127.0.0.1:5432: connect: connection refused","stacktrace":"collection/logger.Error\n\t/home/karn/Desktop/Collection/logger/logger.go:46\ncollection/domain.UserRepositoryDb.AddUser\n\t/home/karn/Desktop/Collection/domain/UserRepository.go:27\ncollection/service.DefaultUserService.NewCollection\n\t/home/karn/Desktop/Collection/service/user.go:45\ncollection/app.UserHandler.CreateCollection\n\t/home/karn/Desktop/Collection/app/register.go:25\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2084\ngithub.com/gorilla/mux.(*Router).ServeHTTP\n\t/home/karn/go/pkg/mod/github.com/gorilla/[email protected]/mux.go:210\nnet/http.serverHandler.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2916\nnet/http.(*conn).serve\n\t/usr/local/go/src/net/http/server.go:1966"}
{"level":"info","timestamp":"2022-09-14T19:04:22.639+0530","caller":"logger/logger.go:38","msg":"Database is Connected"}
{"level":"info","timestamp":"2022-09-14T19:57:48.548+0530","caller":"logger/logger.go:38","msg":"Database is Connected"}
{"level":"info","timestamp":"2022-09-14T19:59:50.968+0530","caller":"logger/logger.go:38","msg":"Database is Connected"}
{"level":"error","timestamp":"2022-09-14T20:00:44.358+0530","caller":"logger/logger.go:46","msg":"Error While creating new account for collection dial tcp 127.0.0.1:5432: connect: connection refused","stacktrace":"collection/logger.Error\n\t/home/karn/Desktop/Collection/logger/logger.go:46\ncollection/domain.UserRepositoryDb.AddUser\n\t/home/karn/Desktop/Collection/domain/UserRepository.go:27\ncollection/service.DefaultUserService.NewCollection\n\t/home/karn/Desktop/Collection/service/user.go:46\ncollection/app.UserHandler.CreateCollection\n\t/home/karn/Desktop/Collection/app/register.go:26\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2084\ngithub.com/gorilla/mux.(*Router).ServeHTTP\n\t/home/karn/go/pkg/mod/github.com/gorilla/[email protected]/mux.go:210\nnet/http.serverHandler.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2916\nnet/http.(*conn).serve\n\t/usr/local/go/src/net/http/server.go:1966"}
{"level":"info","timestamp":"2022-09-14T20:06:24.091+0530","caller":"logger/logger.go:38","msg":"Database is Connected"}
{"level":"error","timestamp":"2022-09-14T20:06:31.147+0530","caller":"logger/logger.go:46","msg":"Error While creating new account for collection dial tcp 127.0.0.1:5432: connect: connection refused","stacktrace":"collection/logger.Error\n\t/home/karn/Desktop/Collection/logger/logger.go:46\ncollection/domain.UserRepositoryDb.AddUser\n\t/home/karn/Desktop/Collection/domain/UserRepository.go:27\ncollection/service.DefaultUserService.NewCollection\n\t/home/karn/Desktop/Collection/service/user.go:46\ncollection/app.UserHandler.CreateCollection\n\t/home/karn/Desktop/Collection/app/register.go:26\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2084\ngithub.com/gorilla/mux.(*Router).ServeHTTP\n\t/home/karn/go/pkg/mod/github.com/gorilla/[email protected]/mux.go:210\nnet/http.serverHandler.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2916\nnet/http.(*conn).serve\n\t/usr/local/go/src/net/http/server.go:1966"}
{"level":"info","timestamp":"2022-09-14T20:07:48.013+0530","caller":"logger/logger.go:38","msg":"Database is Connected"}
{"level":"error","timestamp":"2022-09-14T20:07:50.852+0530","caller":"logger/logger.go:46","msg":"Error While creating new account for collection dial tcp 127.0.0.1:5432: connect: connection refused","stacktrace":"collection/logger.Error\n\t/home/karn/Desktop/Collection/logger/logger.go:46\ncollection/domain.UserRepositoryDb.AddUser\n\t/home/karn/Desktop/Collection/domain/UserRepository.go:27\ncollection/service.DefaultUserService.NewCollection\n\t/home/karn/Desktop/Collection/service/user.go:46\ncollection/app.UserHandler.CreateCollection\n\t/home/karn/Desktop/Collection/app/register.go:26\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2084\ngithub.com/gorilla/mux.(*Router).ServeHTTP\n\t/home/karn/go/pkg/mod/github.com/gorilla/[email protected]/mux.go:210\nnet/http.serverHandler.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2916\nnet/http.(*conn).serve\n\t/usr/local/go/src/net/http/server.go:1966"}
{"level":"info","timestamp":"2022-09-14T20:10:16.873+0530","caller":"logger/logger.go:38","msg":"Database is Connected"}
{"level":"error","timestamp":"2022-09-14T20:10:20.116+0530","caller":"logger/logger.go:46","msg":"Error While creating new account for collection dial tcp 127.0.0.1:5432: connect: connection refused","stacktrace":"collection/logger.Error\n\t/home/karn/Desktop/Collection/logger/logger.go:46\ncollection/domain.UserRepositoryDb.AddUser\n\t/home/karn/Desktop/Collection/domain/UserRepository.go:27\ncollection/service.DefaultUserService.NewCollection\n\t/home/karn/Desktop/Collection/service/user.go:46\ncollection/app.UserHandler.CreateCollection\n\t/home/karn/Desktop/Collection/app/register.go:26\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2084\ngithub.com/gorilla/mux.(*Router).ServeHTTP\n\t/home/karn/go/pkg/mod/github.com/gorilla/[email protected]/mux.go:210\nnet/http.serverHandler.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2916\nnet/http.(*conn).serve\n\t/usr/local/go/src/net/http/server.go:1966"}
{"level":"info","timestamp":"2022-09-14T20:11:12.522+0530","caller":"logger/logger.go:38","msg":"Database is Connected"}
{"level":"error","timestamp":"2022-09-14T20:11:18.057+0530","caller":"logger/logger.go:46","msg":"Error While creating new account for collection dial tcp 127.0.0.1:5432: connect: connection refused","stacktrace":"collection/logger.Error\n\t/home/karn/Desktop/Collection/logger/logger.go:46\ncollection/domain.UserRepositoryDb.AddUser\n\t/home/karn/Desktop/Collection/domain/UserRepository.go:27\ncollection/service.DefaultUserService.NewCollection\n\t/home/karn/Desktop/Collection/service/user.go:46\ncollection/app.UserHandler.CreateCollection\n\t/home/karn/Desktop/Collection/app/register.go:26\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2084\ngithub.com/gorilla/mux.(*Router).ServeHTTP\n\t/home/karn/go/pkg/mod/github.com/gorilla/[email protected]/mux.go:210\nnet/http.serverHandler.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2916\nnet/http.(*conn).serve\n\t/usr/local/go/src/net/http/server.go:1966"}
{"level":"info","timestamp":"2022-09-14T20:11:32.981+0530","caller":"logger/logger.go:38","msg":"Database is Connected"}
{"level":"error","timestamp":"2022-09-14T20:11:39.951+0530","caller":"logger/logger.go:46","msg":"Error While creating new account for collection dial tcp 127.0.0.1:5432: connect: connection refused","stacktrace":"collection/logger.Error\n\t/home/karn/Desktop/Collection/logger/logger.go:46\ncollection/domain.UserRepositoryDb.AddUser\n\t/home/karn/Desktop/Collection/domain/UserRepository.go:27\ncollection/service.DefaultUserService.NewCollection\n\t/home/karn/Desktop/Collection/service/user.go:46\ncollection/app.UserHandler.CreateCollection\n\t/home/karn/Desktop/Collection/app/register.go:26\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2084\ngithub.com/gorilla/mux.(*Router).ServeHTTP\n\t/home/karn/go/pkg/mod/github.com/gorilla/[email protected]/mux.go:210\nnet/http.serverHandler.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2916\nnet/http.(*conn).serve\n\t/usr/local/go/src/net/http/server.go:1966"}
{"level":"info","timestamp":"2022-09-14T20:17:12.357+0530","caller":"logger/logger.go:38","msg":"Database is Connected"}
{"level":"error","timestamp":"2022-09-14T20:17:16.630+0530","caller":"logger/logger.go:46","msg":"Error While creating new account for collection dial tcp 127.0.0.1:5432: connect: connection refused","stacktrace":"collection/logger.Error\n\t/home/karn/Desktop/Collection/logger/logger.go:46\ncollection/domain.UserRepositoryDb.AddUser\n\t/home/karn/Desktop/Collection/domain/UserRepository.go:27\ncollection/service.DefaultUserService.NewCollection\n\t/home/karn/Desktop/Collection/service/user.go:46\ncollection/app.UserHandler.CreateCollection\n\t/home/karn/Desktop/Collection/app/register.go:26\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2084\ngithub.com/gorilla/mux.(*Router).ServeHTTP\n\t/home/karn/go/pkg/mod/github.com/gorilla/[email protected]/mux.go:210\nnet/http.serverHandler.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2916\nnet/http.(*conn).serve\n\t/usr/local/go/src/net/http/server.go:1966"}
{"level":"info","timestamp":"2022-09-14T20:19:38.332+0530","caller":"logger/logger.go:38","msg":"Database is Connected"}
{"level":"error","timestamp":"2022-09-14T20:19:42.321+0530","caller":"logger/logger.go:46","msg":"Error While creating new account for collection dial tcp 127.0.0.1:5432: connect: connection refused","stacktrace":"collection/logger.Error\n\t/home/karn/Desktop/Collection/logger/logger.go:46\ncollection/domain.UserRepositoryDb.AddUser\n\t/home/karn/Desktop/Collection/domain/UserRepository.go:27\ncollection/service.DefaultUserService.NewCollection\n\t/home/karn/Desktop/Collection/service/user.go:46\ncollection/app.UserHandler.CreateCollection\n\t/home/karn/Desktop/Collection/app/register.go:28\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2084\ngithub.com/gorilla/mux.(*Router).ServeHTTP\n\t/home/karn/go/pkg/mod/github.com/gorilla/[email protected]/mux.go:210\nnet/http.serverHandler.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2916\nnet/http.(*conn).serve\n\t/usr/local/go/src/net/http/server.go:1966"}
{"level":"info","timestamp":"2022-09-14T20:22:11.948+0530","caller":"logger/logger.go:38","msg":"Database is Connected"}
{"level":"error","timestamp":"2022-09-14T20:22:15.073+0530","caller":"logger/logger.go:46","msg":"Error While creating new account for collection dial tcp 127.0.0.1:5432: connect: connection refused","stacktrace":"collection/logger.Error\n\t/home/karn/Desktop/Collection/logger/logger.go:46\ncollection/domain.UserRepositoryDb.AddUser\n\t/home/karn/Desktop/Collection/domain/UserRepository.go:27\ncollection/service.DefaultUserService.NewCollection\n\t/home/karn/Desktop/Collection/service/user.go:49\ncollection/app.UserHandler.CreateCollection\n\t/home/karn/Desktop/Collection/app/register.go:27\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2084\ngithub.com/gorilla/mux.(*Router).ServeHTTP\n\t/home/karn/go/pkg/mod/github.com/gorilla/[email protected]/mux.go:210\nnet/http.serverHandler.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2916\nnet/http.(*conn).serve\n\t/usr/local/go/src/net/http/server.go:1966"}
{"level":"info","timestamp":"2022-09-14T20:23:05.393+0530","caller":"logger/logger.go:38","msg":"Database is Connected"}
{"level":"info","timestamp":"2022-09-14T20:24:45.827+0530","caller":"logger/logger.go:38","msg":"Database is Connected"}
{"level":"info","timestamp":"2022-09-14T20:25:18.057+0530","caller":"logger/logger.go:38","msg":"Database is Connected"}
{"level":"error","timestamp":"2022-09-14T20:25:22.011+0530","caller":"logger/logger.go:46","msg":"Error While creating new account for collection dial tcp 127.0.0.1:5432: connect: connection refused","stacktrace":"collection/logger.Error\n\t/home/karn/Desktop/Collection/logger/logger.go:46\ncollection/domain.UserRepositoryDb.AddUser\n\t/home/karn/Desktop/Collection/domain/UserRepository.go:27\ncollection/service.DefaultUserService.NewCollection\n\t/home/karn/Desktop/Collection/service/user.go:48\ncollection/app.UserHandler.CreateCollection\n\t/home/karn/Desktop/Collection/app/register.go:27\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2084\ngithub.com/gorilla/mux.(*Router).ServeHTTP\n\t/home/karn/go/pkg/mod/github.com/gorilla/[email protected]/mux.go:210\nnet/http.serverHandler.ServeHTTP\n\t/usr/local/go/src/net/http/server.go:2916\nnet/http.(*conn).serve\n\t/usr/local/go/src/net/http/server.go:1966"}
17 changes: 8 additions & 9 deletions service/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"collection/logger"
"crypto/rand"
"encoding/json"

// "fmt"
"math/big"
"strconv"

Expand All @@ -17,18 +19,19 @@ import (
)

type UserService interface {
NewCollection(dto.CollectionRequest) (*domain.Collection, *errs.AppError)
NewCollection(dto.CollectionRequest) (*dto.JsonFile, *errs.AppError)
}

type DefaultUserService struct {
repo domain.UserRepositoryDb
}

func (d DefaultUserService) NewCollection(req dto.CollectionRequest) (*domain.Collection, *errs.AppError) {
func (d DefaultUserService) NewCollection(req dto.CollectionRequest)(*dto.JsonFile, *errs.AppError) {
err:=req.ToValidate()
if err!=nil{
return nil,err
}

a := domain.Collection{
User_id: req.User_id,
Name: req.Name,
Expand All @@ -42,7 +45,7 @@ func (d DefaultUserService) NewCollection(req dto.CollectionRequest) (*domain.Co
}

seller := req.Seller
err = d.repo.AddUser(a, seller)
d.repo.AddUser(a, seller)

//Asset File Making by id
var firstAddress string
Expand All @@ -51,7 +54,7 @@ func (d DefaultUserService) NewCollection(req dto.CollectionRequest) (*domain.Co
break
}

c := dto.JsonFile{
c := &dto.JsonFile{
Price: uint32(req.Mint_price),
Number: int(req.Total_supply),
SolTreasuryAccount: firstAddress,
Expand Down Expand Up @@ -136,14 +139,10 @@ func (d DefaultUserService) NewCollection(req dto.CollectionRequest) (*domain.Co
f, _ := json.MarshalIndent(e, " ", " ")
jso.Write(f)
imgPath := configFilePath+"/"+strconv.Itoa(i)+".png"
// img, error := os.OpenFile(imgPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
// if error != nil {
// return nil, errs.NewStatusInternalServerError("file is not opening")
// }
dec:= dto.Load("dto/hdpng/pacman.png")
dto.Save(imgPath,dec)
}
return nil, err
return c, err
}

func NewUserService(repository domain.UserRepositoryDb) DefaultUserService {
Expand Down

0 comments on commit e7b70d0

Please sign in to comment.