Skip to content

Commit

Permalink
Wishlist (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayaanqui authored Feb 18, 2024
1 parent f337e8e commit e863a64
Show file tree
Hide file tree
Showing 17 changed files with 604 additions and 32 deletions.
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/bep/godartsass v0.16.0 // indirect
github.com/bep/golibsass v1.1.0 // indirect
github.com/cli/safeexec v1.0.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
github.com/creack/pty v1.1.18 // indirect
github.com/cubicdaiya/gonp v1.0.4 // indirect
github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 // indirect
Expand All @@ -29,6 +30,7 @@ require (
github.com/gohugoio/hugo v0.111.3 // indirect
github.com/google/cel-go v0.18.2 // indirect
github.com/gosimple/unidecode v1.0.1 // indirect
github.com/gzuidhof/tygo v0.2.13 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
Expand All @@ -44,12 +46,14 @@ require (
github.com/pingcap/tidb/pkg/parser v0.0.0-20231103154709-4f00ece106b1 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/riza-io/grpc-go v0.2.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/spf13/afero v1.9.3 // indirect
github.com/spf13/cobra v1.8.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stoewer/go-strcase v1.2.0 // indirect
github.com/tdewolff/parse/v2 v2.6.5 // indirect
github.com/tetratelabs/wazero v1.6.0 // indirect
github.com/urfave/cli v1.22.12 // indirect
github.com/wasilibs/go-pgquery v0.0.0-20231208014744-de63626a1e99 // indirect
github.com/wasilibs/wazerox v0.0.0-20231208014050-e6b725634531 // indirect
go.uber.org/atomic v1.11.0 // indirect
Expand All @@ -60,6 +64,7 @@ require (
golang.org/x/tools v0.14.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231120223509-83a465c0220f // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/uint128 v1.2.0 // indirect
modernc.org/cc/v3 v3.40.0 // indirect
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk=
dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/andybalholm/brotli v1.0.6 h1:Yf9fFpf49Zrxb9NlQaluyE92/+X7UVHlhMNJN2sxfOI=
github.com/andybalholm/brotli v1.0.6/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
Expand All @@ -67,6 +68,8 @@ github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnht
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
github.com/cosmtrek/air v1.49.0 h1:/LfwnLquRoRyUO/K+m/6N5YpUkxcKcDGq2dH7RYbT9c=
github.com/cosmtrek/air v1.49.0/go.mod h1:yOz9vy7edZ75KRN9+Ofqwm3OU0wuv4Csc+ikMeZxxS8=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.3 h1:qMCsGGgs+MAzDFyp9LpAe1Lqy/fY/qCovCm0qnXZOBM=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY=
Expand Down Expand Up @@ -202,6 +205,8 @@ github.com/gosimple/slug v1.13.1 h1:bQ+kpX9Qa6tHRaK+fZR0A0M2Kd7Pa5eHPPsb1JpHD+Q=
github.com/gosimple/slug v1.13.1/go.mod h1:UiRaFH+GEilHstLUmcBgWcI42viBN7mAb818JrYOeFQ=
github.com/gosimple/unidecode v1.0.1 h1:hZzFTMMqSswvf0LBJZCZgThIZrpDHFXux9KeGmn6T/o=
github.com/gosimple/unidecode v1.0.1/go.mod h1:CP0Cr1Y1kogOtx0bJblKzsVWrqYaqfNOnHzpgWw4Awc=
github.com/gzuidhof/tygo v0.2.13 h1:aCPwb+N5Oh5pP3HiC/3a3Jwq4QXmMZPhRMCZu927yHg=
github.com/gzuidhof/tygo v0.2.13/go.mod h1:s3lpnppkDixQQhMWD78yPtAmugMHENsPWpQYziUIpw0=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
Expand Down Expand Up @@ -279,6 +284,7 @@ github.com/riza-io/grpc-go v0.2.0 h1:2HxQKFVE7VuYstcJ8zqpN84VnAoJ4dCL6YFhJewNcHQ
github.com/riza-io/grpc-go v0.2.0/go.mod h1:2bDvR9KkKC3KhtlSHfR3dAXjUMT86kg4UfWFyVGWqi8=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/spf13/afero v1.9.3 h1:41FoI0fD7OR7mGcKE/aOiLkGreyf8ifIOQmJANWogMk=
Expand Down Expand Up @@ -309,6 +315,8 @@ github.com/tdewolff/parse/v2 v2.6.5/go.mod h1:woz0cgbLwFdtbjJu8PIKxhW05KplTFQkOd
github.com/tdewolff/test v1.0.7/go.mod h1:6DAvZliBAAnD7rhVgwaM7DE5/d9NMOAJ09SqYqeK4QE=
github.com/tetratelabs/wazero v1.6.0 h1:z0H1iikCdP8t+q341xqepY4EWvHEw8Es7tlqiVzlP3g=
github.com/tetratelabs/wazero v1.6.0/go.mod h1:0U0G41+ochRKoPKCJlh0jMg1CHkyfK8kDqiirMmKY8A=
github.com/urfave/cli v1.22.12 h1:igJgVw1JdKH+trcLWLeLwZjU9fEfPesQ+9/e4MQ44S8=
github.com/urfave/cli v1.22.12/go.mod h1:sSBEIC79qR6OvcmsD4U3KABeOTxDqQtdDnaFuUN30b8=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.51.0 h1:8b30A5JlZ6C7AS81RsWjYMQmrZG6feChmgAolCl1SqA=
Expand Down Expand Up @@ -694,6 +702,8 @@ gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYs
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
10 changes: 10 additions & 0 deletions src/controllers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ func New(app_ctx types.AppContext, querier *database.Queries, service services.S
participants.Get("/:event_id/:participant_id", c.UseJwtAuth, c.UseEventAuthWithParam, c.UseEventParticipantAuthWithParam, c.GetParticipantById)
participants.Patch("/:event_id/:participant_id", c.UseJwtAuth, c.UseEventAuthWithParam, c.UseEventParticipantAuthWithParam, c.UpdateMeParticipant)
}
wishes := server.Group("/wishes")
{
wishes.Post("/:event_id", c.UseJwtAuth, c.UseEventAuthWithParam, c.CreateWish)
wishes.Delete("/:event_id", c.UseJwtAuth, c.UseEventAuthWithParam, c.DeleteWish)
wishes.Get("/:event_id/:participant_id", c.UseJwtAuth, c.UseEventAuthWithParam, c.UseEventParticipantAuthWithParam, c.GetWishes)
}
server.Get("*", func(c *fiber.Ctx) error {
return utils.ResponseWithStatusCode(c, fiber.ErrNotFound.Code, types.Errors{
Errors: []string{"resource not found"},
Expand Down Expand Up @@ -100,3 +106,7 @@ func GetEventIdFromContext(user_context context.Context) int64 {
id := user_context.Value(EVENT_ID_PARAM_KEY).(int64)
return id
}

func GetParticipantFromContext(user_context context.Context) database.Participant {
return user_context.Value(PARTICIPANT_OB_KEY).(database.Participant)
}
30 changes: 6 additions & 24 deletions src/controllers/products_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/giftxtrade/api/src/database"
"github.com/giftxtrade/api/src/mappers"
"github.com/giftxtrade/api/src/types"
"github.com/giftxtrade/api/src/utils"
"github.com/gofiber/fiber/v2"
Expand Down Expand Up @@ -58,27 +59,7 @@ func (ctr Controller) FindAllProducts(c *fiber.Ctx) error {
}
mapped_products := make([]types.Product, len(products))
for i, p := range products {
mapped_products[i] = types.Product{
ID: p.Product.ID,
Title: p.Product.Title,
Description: p.Product.Description.String,
ProductKey: p.Product.ProductKey,
ImageUrl: p.Product.ImageUrl,
TotalReviews: p.Product.TotalReviews,
Rating: p.Product.Rating,
Price: p.Product.Price,
Currency: string(p.Product.Currency),
Url: p.Product.Url,
CategoryID: p.Product.CategoryID.Int64,
Category: types.Category{
ID: p.Category.ID,
Name: p.Category.Name,
Description: p.Category.Description.String,
},
CreatedAt: p.Product.CreatedAt,
UpdatedAt: p.Product.UpdatedAt,
Origin: p.Product.Origin,
}
mapped_products[i] = mappers.DbProductToProduct(p.Product, &p.Category)
}
return utils.DataResponse(c, mapped_products)
}
Expand All @@ -94,10 +75,11 @@ func (ctr Controller) CreateProduct(c *fiber.Ctx) error {
if err != nil {
return utils.FailResponse(c, "could not create/update product")
}
mapped_product := mappers.DbProductToProduct(product, nil)
if created {
return utils.DataResponseCreated(c, product)
return utils.DataResponseCreated(c, mapped_product)
}
return utils.DataResponse(c, product)
return utils.DataResponse(c, mapped_product)
}

// [GET] /products/:id
Expand All @@ -111,5 +93,5 @@ func (ctr Controller) FindProduct(c *fiber.Ctx) error {
if err != nil {
return utils.FailResponse(c, "product not found")
}
return utils.DataResponse(c, product)
return utils.DataResponse(c, mappers.DbProductToProduct(product, nil))
}
133 changes: 133 additions & 0 deletions src/controllers/wishes_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package controllers

import (
"database/sql"

"github.com/giftxtrade/api/src/database"
"github.com/giftxtrade/api/src/mappers"
"github.com/giftxtrade/api/src/types"
"github.com/giftxtrade/api/src/utils"
"github.com/gofiber/fiber/v2"
)

func (ctr *Controller) CreateWish(c *fiber.Ctx) error {
input, err := utils.ParseAndValidateBody[types.CreateWish](ctr.Validator, c.Body())
if err != nil {
return utils.FailResponse(c, err.Error())
}

auth := GetAuthContext(c.UserContext())
event_id := GetEventIdFromContext(c.UserContext())
participant, err := ctr.Querier.FindParticipantFromEventIdAndUser(c.Context(), database.FindParticipantFromEventIdAndUserParams{
EventID: event_id,
UserID: sql.NullInt64{
Int64: auth.User.ID,
Valid: true,
},
})
if err != nil {
return utils.FailResponse(c, "participant does not exist on the event")
}
create_wish_params := database.CreateWishParams{
UserID: auth.User.ID,
EventID: event_id,
ParticipantID: participant.ID,
}
var product *database.Product = nil
if input.ProductID != nil {
// check if product id is valid
product_id := *input.ProductID
p, err := ctr.Querier.FindProductById(c.Context(), product_id)
if err != nil {
return utils.FailResponse(c, "invalid product id")
}
product = &p
create_wish_params.ProductID = sql.NullInt64{
Int64: product_id,
Valid: true,
}

// check if wish with `product_id` already exists and update `quantity`
existing_wish, existing_wish_err := ctr.Querier.GetWishWithProductID(c.Context(), database.GetWishWithProductIDParams{
UserID: auth.User.ID,
EventID: event_id,
ParticipantID: participant.ID,
ProductID: sql.NullInt64{
Valid: true,
Int64: product_id,
},
})
if existing_wish_err == nil {
updated_wish, update_wish_err := ctr.Querier.UpdateWishQuantity(c.Context(), database.UpdateWishQuantityParams{
ID: existing_wish.ID,
Quantity: existing_wish.Quantity + 1,
})
if update_wish_err != nil {
return utils.FailResponse(c, "could not update wish quantity")
}
return utils.DataResponse(c, mappers.DbWishToWish(updated_wish, product))
}
}

wish, err := ctr.Querier.CreateWish(c.Context(), create_wish_params)
if err != nil {
return utils.FailResponse(c, "could not create wish")
}
return utils.DataResponseCreated(c, mappers.DbWishToWish(wish, product))
}

func (ctr *Controller) DeleteWish(c *fiber.Ctx) error {
input, err := utils.ParseAndValidateBody[types.DeleteWish](ctr.Validator, c.Body())
if err != nil {
return utils.FailResponse(c, err.Error())
}

auth := GetAuthContext(c.UserContext())
event_id := GetEventIdFromContext(c.UserContext())
participant, err := ctr.Querier.FindParticipantFromEventIdAndUser(c.Context(), database.FindParticipantFromEventIdAndUserParams{
EventID: event_id,
UserID: sql.NullInt64{
Int64: auth.User.ID,
Valid: true,
},
})
if err != nil {
return utils.FailResponse(c, "participant does not exist on the event")
}

wish, err := ctr.Querier.GetWishByAllIDs(c.Context(), database.GetWishByAllIDsParams{
ID: input.WishID,
UserID: auth.User.ID,
ParticipantID: participant.ID,
EventID: event_id,
})
if err != nil {
return utils.FailResponse(c, "could not find wish with the given inputs")
}

_, err = ctr.Querier.DeleteWish(c.Context(), wish.ID)
if err != nil {
return utils.FailResponse(c, "could not delete wish")
}
return utils.DataResponse(c, mappers.DbWishToWish(wish, nil))
}

func (ctr *Controller) GetWishes(c *fiber.Ctx) error {
auth := GetAuthContext(c.UserContext())
event_id := GetEventIdFromContext(c.UserContext())
participant := GetParticipantFromContext(c.UserContext())
wishes, err := ctr.Querier.GetAllWishesForUser(c.Context(), database.GetAllWishesForUserParams{
UserID: auth.User.ID,
EventID: event_id,
ParticipantID: participant.ID,
})
if err != nil {
return utils.FailResponse(c, "could not fetch wishes")
}

mapped_wishes := make([]types.Wish, len(wishes))
for i, w := range wishes {
mapped_wishes[i] = mappers.DbWishToWish(w.Wish, &w.Product)
}
return utils.DataResponse(c, mapped_wishes)
}
Loading

0 comments on commit e863a64

Please sign in to comment.