From e7b70d062025a82d93118f6da261f508591faf91 Mon Sep 17 00:00:00 2001 From: Karan Date: Wed, 14 Sep 2022 20:26:53 +0530 Subject: [PATCH] Initial Commit --- app/register.go | 39 +++++++++++---------- domain/UserRepository.go | 9 +++-- dto/user.go | 74 +++++++++++++++++----------------------- log.json | 23 +++++++++++++ service/user.go | 17 +++++---- 5 files changed, 90 insertions(+), 72 deletions(-) diff --git a/app/register.go b/app/register.go index 8c5dd38..8fccc72 100644 --- a/app/register.go +++ b/app/register.go @@ -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) - } -} \ No newline at end of file +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) + } +} diff --git a/domain/UserRepository.go b/domain/UserRepository.go index 77e5178..2023b0d 100644 --- a/domain/UserRepository.go +++ b/domain/UserRepository.go @@ -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) @@ -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 diff --git a/dto/user.go b/dto/user.go index cdfc3f7..b86fdcb 100644 --- a/dto/user.go +++ b/dto/user.go @@ -2,7 +2,6 @@ package dto import ( "collection/errs" - "fmt" "time" "gopkg.in/guregu/null.v4" @@ -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 { @@ -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 - -} \ No newline at end of file + } + + 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 + +} diff --git a/log.json b/log.json index 4369c16..f133dea 100644 --- a/log.json +++ b/log.json @@ -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/mux@v1.8.0/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/mux@v1.8.0/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/mux@v1.8.0/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/mux@v1.8.0/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/mux@v1.8.0/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/mux@v1.8.0/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/mux@v1.8.0/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/mux@v1.8.0/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/mux@v1.8.0/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/mux@v1.8.0/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/mux@v1.8.0/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"} diff --git a/service/user.go b/service/user.go index eb6effd..2a52ce4 100644 --- a/service/user.go +++ b/service/user.go @@ -7,6 +7,8 @@ import ( "collection/logger" "crypto/rand" "encoding/json" + + // "fmt" "math/big" "strconv" @@ -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, @@ -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 @@ -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, @@ -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 {