Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
mirzahilmi committed Apr 27, 2024
2 parents b20411a + d19ad2b commit d93851a
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func main() {
animalRepo := repository.NewAnimalRepository(db)
animalUsecase := usecase.NewAnimalUsecase(userRepo, animalRepo)
rest.RegisterAnimalHandler(animalUsecase, validator, api)
campaignUsecase := usecase.NewCampaignUsecase(campaignRepo, log, supabaseClient)
campaignUsecase := usecase.NewCampaignUsecase(campaignRepo, userRepo, log, supabaseClient)
rest.RegisterCampaignHandler(campaignUsecase, api, validator)
reportUsecase := usecase.NewReportUsecase(reportRepo, userRepo, supabaseClient)
rest.RegisterReportHandler(reportUsecase, validator, api)
Expand Down
1 change: 1 addition & 0 deletions db/migrations/20240427032748_campaign_submissions.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ CREATE TABLE CampaignSubmissions (
UserID BIGINT UNSIGNED NOT NULL,
CampaignID BIGINT UNSIGNED NOT NULL,
Submission TEXT NOT NULL,
Status ENUM('PENDING', 'APPROVED', 'REJECTED') NOT NULL DEFAULT 'PENDING',
CreatedAt DATETIME NOT NULL DEFAULT NOW(),
FOREIGN KEY FKCampaignSubmissionsUsers (UserID) REFERENCES Users (ID) ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY FKCampaignSubmissionsCampaigns (CampaignID) REFERENCES Campaigns (ID) ON UPDATE CASCADE ON DELETE CASCADE
Expand Down
29 changes: 29 additions & 0 deletions internal/delivery/rest/campaign.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func RegisterCampaignHandler(
router.Delete("/:id<int>", middleware.BearerAuth("true"), campaignHandler.Delete)
router.Get("/_admin", middleware.BearerAuth("true"), campaignHandler.GetAllForAdmin)
router.Get("/_admin/:id<int>", middleware.BearerAuth("true"), campaignHandler.GetByIDForAdmin)
router.Get("/_submissions", middleware.BearerAuth("true"), campaignHandler.GetAllCampaignSubmission)
router.Patch("/_submissions/:id<int>/:userID<int>", middleware.BearerAuth("true"), campaignHandler.UpdateStatusCampaignSubmission)
}

func (h *campaignHandler) FetchAll(c *fiber.Ctx) error {
Expand Down Expand Up @@ -178,3 +180,30 @@ func (h *campaignHandler) GetByIDForAdmin(c *fiber.Ctx) error {
}
return c.Status(fiber.StatusOK).JSON(campaign)
}

func (h *campaignHandler) GetAllCampaignSubmission(c *fiber.Ctx) error {
campaigns, err := h.usecase.GetAllCampaignSubmission(c.Context())
if err != nil {
return err
}
return c.Status(fiber.StatusOK).JSON(campaigns)
}

func (h *campaignHandler) UpdateStatusCampaignSubmission(c *fiber.Ctx) error {
id, err := strconv.ParseInt(c.Params("id"), 10, 64)
if err != nil {
return err
}

userID, err := strconv.ParseInt(c.Params("userID"), 10, 64)
if err != nil {
return err
}

value := c.Query("status")
err = h.usecase.UpdateStatusCampaignSubmission(c.Context(), id, userID, value)
if err != nil {
return err
}
return c.Status(fiber.StatusOK).JSON(fiber.Map{"message": "Submission updated"})
}
1 change: 1 addition & 0 deletions internal/model/campaign.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ type CampaignSubmission struct {
UserID int64 `db:"UserID" json:"userId"`
CampaignID int64 `db:"CampaignID" json:"campaignId"`
Submission string `db:"Submission" json:"submission"`
Status string `db:"Status" json:"status"`
}
32 changes: 32 additions & 0 deletions internal/repository/campaign.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ type CampaignQueryerItf interface {
GetByID(ctx context.Context, id int64) (model.Campaign, error)
GetAll(ctx context.Context) ([]model.Campaign, error)
CreateSubmission(ctx context.Context, campaignSubmission model.CampaignSubmission) error
GetAllCampaignSubmission(ctx context.Context) ([]model.CampaignSubmission, error)
UpdateStatusCampaignSubmission(ctx context.Context, submission model.CampaignSubmission) error
GetCampaignSubmissionByID(ctx context.Context, id int64, userID int64) (model.CampaignSubmission, error)
}

type campaignQueryer struct {
Expand Down Expand Up @@ -155,3 +158,32 @@ func (q *campaignQueryer) CreateSubmission(ctx context.Context, campaignSubmissi
_, err = q.ext.ExecContext(ctx, query, args...)
return err
}

func (q *campaignQueryer) GetAllCampaignSubmission(ctx context.Context) ([]model.CampaignSubmission, error) {
var submissions []model.CampaignSubmission
if err := sqlx.SelectContext(ctx, q.ext, &submissions, qGetAllCampaignSubmission); err != nil {
return nil, err
}
return submissions, nil
}

func (q *campaignQueryer) UpdateStatusCampaignSubmission(ctx context.Context, submission model.CampaignSubmission) error {
query, args, err := sqlx.Named(qUpdateStatusCampaignSubmission, fiber.Map{
"UserID": submission.UserID,
"CampaignID": submission.CampaignID,
"Status": submission.Status,
})
if err != nil {
return err
}
_, err = q.ext.ExecContext(ctx, query, args...)
return err
}

func (q *campaignQueryer) GetCampaignSubmissionByID(ctx context.Context, id int64, userID int64) (model.CampaignSubmission, error) {
var submission model.CampaignSubmission
if err := sqlx.GetContext(ctx, q.ext, &submission, qGetCampaignSubmissionByID, id, userID); err != nil {
return model.CampaignSubmission{}, err
}
return submission, nil
}
22 changes: 22 additions & 0 deletions internal/repository/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,28 @@ const (
(CampaignID, UserID, Submission)
VALUE
(:CampaignID, :UserID, :Submission);`
qGetAllCampaignSubmission = `
SELECT
CampaignID,
UserID,
Submission,
Status
FROM CampaignSubmissions
`
qUpdateStatusCampaignSubmission = `
UPDATE CampaignSubmissions
SET Status = :Status
WHERE CampaignID = :CampaignID AND UserID = :UserID;
`
qGetCampaignSubmissionByID = `
SELECT
CampaignID,
UserID,
Submission
FROM CampaignSubmissions
WHERE CampaignID = ? AND UserID = ?
LIMIT 1;
`

// Exchanges
qCreateExchange = `
Expand Down
53 changes: 52 additions & 1 deletion internal/usecase/campaign.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,24 @@ type CampaignUsecaseItf interface {
GetByID(ctx context.Context, id int64) (model.Campaign, error)
GetAll(ctx context.Context) ([]model.Campaign, error)
SubmitSubmission(ctx context.Context, id int64, req model.CampaignSubmissionRequest) error
GetAllCampaignSubmission(ctx context.Context) ([]model.CampaignSubmission, error)
UpdateStatusCampaignSubmission(ctx context.Context, id int64, userID int64, value string) error
}

type campaignUsecase struct {
campaignRepo repository.CampaignRepositoryItf
userRepo repository.UserRepositoryItf
log *logrus.Logger
supabase *storage_go.Client
}

func NewCampaignUsecase(
campaignRepo repository.CampaignRepositoryItf,
userRepo repository.UserRepositoryItf,
log *logrus.Logger,
supabase *storage_go.Client,
) CampaignUsecaseItf {
return &campaignUsecase{campaignRepo, log, supabase}
return &campaignUsecase{campaignRepo, userRepo, log, supabase}
}

func (u *campaignUsecase) FetchAll(ctx context.Context) ([]model.Campaign, error) {
Expand Down Expand Up @@ -198,3 +202,50 @@ func (u *campaignUsecase) SubmitSubmission(ctx context.Context, id int64, req mo
}
return client.Commit()
}

func (u *campaignUsecase) GetAllCampaignSubmission(ctx context.Context) ([]model.CampaignSubmission, error) {
client, err := u.campaignRepo.NewClient(false, nil)
if err != nil {
return nil, err
}
submissions, err := client.GetAllCampaignSubmission(ctx)
if err != nil {
return nil, err
}
return submissions, nil
}

func (u *campaignUsecase) UpdateStatusCampaignSubmission(ctx context.Context, id int64, userID int64, value string) error {
campaignClient, err := u.campaignRepo.NewClient(true, nil)
if err != nil {
return err
}
defer campaignClient.Rollback()

submission, err := campaignClient.GetCampaignSubmissionByID(ctx, id, userID)
if err != nil {
return err
}

userClient, err := u.userRepo.NewClient(true, nil)
if err != nil {
return err
}
defer userClient.Rollback()

if value == "APPROVED" {
if err := userClient.UpdateBalance(ctx, userID, 100); err != nil {
return err
}
}

submission.Status = value
err = campaignClient.UpdateStatusCampaignSubmission(ctx, submission)
if err != nil {
return err
}
if err := campaignClient.Commit(); err != nil {
return err
}
return userClient.Commit()
}
5 changes: 2 additions & 3 deletions internal/usecase/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,7 @@ func (h *userUsecase) Exchange(ctx context.Context, exchange model.ExchangeReque
return ErrInsufficientBalance
}

user.Balance -= int(exchange.Amount)
if err := userClient.UpdateBalance(ctx, ctx.Value(ClientID).(int64), user.Balance); err != nil {
if err := userClient.UpdateBalance(ctx, ctx.Value(ClientID).(int64), -int(exchange.Amount)); err != nil {
return err
}

Expand All @@ -194,7 +193,7 @@ func (h *userUsecase) Exchange(ctx context.Context, exchange model.ExchangeReque
return err
}

return nil
return userClient.Commit()
}

func (e *userUsecase) GetExchanges(ctx context.Context) ([]model.ExchangeCleanResource, error) {
Expand Down

0 comments on commit d93851a

Please sign in to comment.