diff --git a/module/entities/users_models.go b/module/entities/users_models.go index 6d3df2d..8c0ca1e 100644 --- a/module/entities/users_models.go +++ b/module/entities/users_models.go @@ -3,23 +3,24 @@ package entities import "time" type UserModels struct { - ID uint64 `gorm:"column:id;type:BIGINT UNSIGNED;primaryKey" json:"id"` - Email string `gorm:"column:email;type:VARCHAR(255)" json:"email"` - Password string `gorm:"column:password;type:VARCHAR(255)" json:"password"` - Phone string `gorm:"column:phone;type:VARCHAR(255)" json:"phone"` - Role string `gorm:"column:role;type:VARCHAR(255)" json:"role"` - Name string `gorm:"column:name;type:VARCHAR(255)" json:"name"` - PhotoProfile string `gorm:"column:photo_profile;type:VARCHAR(255)" json:"photo_profile"` - TotalGram uint64 `gorm:"column:total_gram;type:BIGINT UNSIGNED" json:"total_gram"` - Level string `gorm:"column:level;type:VARCHAR(255)" json:"level"` - Exp uint64 `gorm:"column:exp;type:BIGINT UNSIGNED" json:"exp"` - IsVerified bool `gorm:"column:is_verified;default:false" json:"is_verified"` - CreatedAt time.Time `gorm:"column:created_at;type:TIMESTAMP" json:"created_at"` - UpdatedAt time.Time `gorm:"column:updated_at;type:TIMESTAMP" json:"updated_at"` - DeletedAt *time.Time `gorm:"column:deleted_at;type:TIMESTAMP NULL;index" json:"deleted_at"` - Address []AddressModels `gorm:"foreignKey:UserID" json:"addresses"` - Articles []ArticleModels `gorm:"many2many:user_bookmarks;" json:"users"` - Reviews []ReviewModels `gorm:"foreignKey:UserID" json:"reviews"` + ID uint64 `gorm:"column:id;type:BIGINT UNSIGNED;primaryKey" json:"id"` + Email string `gorm:"column:email;type:VARCHAR(255)" json:"email"` + Password string `gorm:"column:password;type:VARCHAR(255)" json:"password"` + Phone string `gorm:"column:phone;type:VARCHAR(255)" json:"phone"` + Role string `gorm:"column:role;type:VARCHAR(255)" json:"role"` + Name string `gorm:"column:name;type:VARCHAR(255)" json:"name"` + PhotoProfile string `gorm:"column:photo_profile;type:VARCHAR(255)" json:"photo_profile"` + TotalGram uint64 `gorm:"column:total_gram;type:BIGINT UNSIGNED" json:"total_gram"` + TotalChallenge uint64 `gorm:"column:total_challenge;type:BIGINT UNSIGNED" json:"total_challenge"` + Level string `gorm:"column:level;type:VARCHAR(255)" json:"level"` + Exp uint64 `gorm:"column:exp;type:BIGINT UNSIGNED" json:"exp"` + IsVerified bool `gorm:"column:is_verified;default:false" json:"is_verified"` + CreatedAt time.Time `gorm:"column:created_at;type:TIMESTAMP" json:"created_at"` + UpdatedAt time.Time `gorm:"column:updated_at;type:TIMESTAMP" json:"updated_at"` + DeletedAt *time.Time `gorm:"column:deleted_at;type:TIMESTAMP NULL;index" json:"deleted_at"` + Address []AddressModels `gorm:"foreignKey:UserID" json:"addresses"` + Articles []ArticleModels `gorm:"many2many:user_bookmarks;" json:"users"` + Reviews []ReviewModels `gorm:"foreignKey:UserID" json:"reviews"` } type AddressModels struct { diff --git a/module/feature/challenge/dto/response.go b/module/feature/challenge/dto/response.go index 86b8163..2691018 100644 --- a/module/feature/challenge/dto/response.go +++ b/module/feature/challenge/dto/response.go @@ -17,8 +17,8 @@ type ChallengeFormatter struct { Exp uint64 `json:"exp"` } -func FormatChallenge(challenge entities.ChallengeModels) ChallengeFormatter { - challengeFormatter := ChallengeFormatter{} +func FormatChallenge(challenge *entities.ChallengeModels) *ChallengeFormatter { + challengeFormatter := &ChallengeFormatter{} challengeFormatter.ID = challenge.ID challengeFormatter.Title = challenge.Title challengeFormatter.Photo = challenge.Photo @@ -31,8 +31,8 @@ func FormatChallenge(challenge entities.ChallengeModels) ChallengeFormatter { return challengeFormatter } -func FormatterChallenge(challenge []entities.ChallengeModels) []ChallengeFormatter { - var challengeFormatter []ChallengeFormatter +func FormatterChallenge(challenge []*entities.ChallengeModels) []*ChallengeFormatter { + var challengeFormatter []*ChallengeFormatter for _, challenge := range challenge { FormatChallenge := FormatChallenge(challenge) @@ -53,8 +53,8 @@ type ChallengeFormFormatter struct { CreatedAt time.Time `json:"tanggal_berpartisipasi"` } -func FormatChallengeForm(form entities.ChallengeFormModels, exp uint64, createdAt time.Time) ChallengeFormFormatter { - challengeFormFormatter := ChallengeFormFormatter{} +func FormatChallengeForm(form *entities.ChallengeFormModels, exp uint64, createdAt time.Time) *ChallengeFormFormatter { + challengeFormFormatter := &ChallengeFormFormatter{} challengeFormFormatter.ID = form.ID challengeFormFormatter.UserID = form.UserID challengeFormFormatter.ChallengeID = form.ChallengeID @@ -67,8 +67,8 @@ func FormatChallengeForm(form entities.ChallengeFormModels, exp uint64, createdA return challengeFormFormatter } -func FormatterChallengeForm(forms []entities.ChallengeFormModels, exp uint64, createdAt time.Time) []ChallengeFormFormatter { - var formFormatter []ChallengeFormFormatter +func FormatterChallengeForm(forms []*entities.ChallengeFormModels, exp uint64, createdAt time.Time) []*ChallengeFormFormatter { + var formFormatter []*ChallengeFormFormatter for _, form := range forms { formattedForm := FormatChallengeForm(form, exp, createdAt) formFormatter = append(formFormatter, formattedForm) diff --git a/module/feature/challenge/handler/handler.go b/module/feature/challenge/handler/handler.go index 3ab3105..a3f4170 100644 --- a/module/feature/challenge/handler/handler.go +++ b/module/feature/challenge/handler/handler.go @@ -30,7 +30,7 @@ func (h *ChallengeHandler) GetAllChallenges() echo.HandlerFunc { pageConv, _ := strconv.Atoi(strconv.Itoa(page)) perPage := 8 - var challenge []entities.ChallengeModels + var challenge []*entities.ChallengeModels var err error search := c.QueryParam("search") if search != "" { @@ -39,11 +39,10 @@ func (h *ChallengeHandler) GetAllChallenges() echo.HandlerFunc { challenge, _, err = h.service.GetAllChallenges(pageConv, perPage) } if err != nil { - c.Logger().Error("handler: failed to fetch all challenge:", err.Error()) return response.SendErrorResponse(c, http.StatusInternalServerError, "Internal Server Error") } - var activeChallenges []entities.ChallengeModels + var activeChallenges []*entities.ChallengeModels for _, ch := range challenge { if ch.DeletedAt == nil { activeChallenges = append(activeChallenges, ch) @@ -95,7 +94,7 @@ func (h *ChallengeHandler) CreateChallenge() echo.HandlerFunc { return response.SendErrorResponse(c, http.StatusBadRequest, "Tanggal mulai tidak dapat setelah tanggal selesai.") } - newChallenge := entities.ChallengeModels{ + newChallenge := &entities.ChallengeModels{ Title: challengeRequest.Title, Photo: uploadedURL, StartDate: challengeRequest.StartDate, @@ -145,7 +144,7 @@ func (h *ChallengeHandler) UpdateChallenge() echo.HandlerFunc { } } - updatedChallenge := entities.ChallengeModels{ + updatedChallenge := &entities.ChallengeModels{ ID: challengeID, Title: updateRequest.Title, Photo: uploadedURL, @@ -241,10 +240,10 @@ func (h *ChallengeHandler) CreateSubmitChallengeForm() echo.HandlerFunc { createdForm, err := h.service.CreateSubmitChallengeForm(&newForm) if err != nil { - return response.SendErrorResponse(c, http.StatusInternalServerError, "Kesalahan Server Internal: "+err.Error()) + return response.SendErrorResponse(c, http.StatusInternalServerError, "Gagal mengikuti tantangan: "+err.Error()) } - return response.SendSuccessResponse(c, "Berhasil mengikuti tantangan", dto.FormatChallengeForm(*createdForm, challenge.Exp, createdForm.CreatedAt)) + return response.SendSuccessResponse(c, "Berhasil mengikuti tantangan", dto.FormatChallengeForm(createdForm, challenge.Exp, createdForm.CreatedAt)) } } @@ -254,7 +253,7 @@ func (h *ChallengeHandler) GetAllSubmitChallengeForm() echo.HandlerFunc { pageConv, _ := strconv.Atoi(strconv.Itoa(page)) perPage := 8 - var partisipan []entities.ChallengeFormModels + var partisipan []*entities.ChallengeFormModels var totalItems int64 var err error filterStatus := c.QueryParam("status") @@ -312,7 +311,6 @@ func (h *ChallengeHandler) UpdateSubmitChallengeForm() echo.HandlerFunc { } _, err = h.service.UpdateSubmitChallengeForm(formID, formRequest) if err != nil { - c.Logger().Error("handler: failed create voucher:", err.Error()) return response.SendErrorResponse(c, http.StatusInternalServerError, "Kesalahan Server Internal: "+err.Error()) } return response.SendStatusOkResponse(c, "form berhasil dirubah") diff --git a/module/feature/challenge/interface.go b/module/feature/challenge/interface.go index d42b738..f472cc2 100644 --- a/module/feature/challenge/interface.go +++ b/module/feature/challenge/interface.go @@ -7,42 +7,38 @@ import ( ) type RepositoryChallengeInterface interface { - FindAll(page, perpage int) ([]entities.ChallengeModels, error) + FindAll(page, perpage int) ([]*entities.ChallengeModels, error) GetTotalChallengeCount() (int64, error) - FindByTitle(page, perpage int, title string) ([]entities.ChallengeModels, error) + FindByTitle(page, perpage int, title string) ([]*entities.ChallengeModels, error) GetTotalChallengeCountByTitle(title string) (int64, error) - CreateChallenge(challenge entities.ChallengeModels) (entities.ChallengeModels, error) - UpdateChallenge(id uint64, updatedChallenge entities.ChallengeModels) (entities.ChallengeModels, error) - GetChallengeById(id uint64) (entities.ChallengeModels, error) + CreateChallenge(challenge *entities.ChallengeModels) (*entities.ChallengeModels, error) + UpdateChallenge(id uint64, updatedChallenge *entities.ChallengeModels) (*entities.ChallengeModels, error) + GetChallengeById(id uint64) (*entities.ChallengeModels, error) DeleteChallenge(id uint64) error - - //partisipan CreateSubmitChallengeForm(form *entities.ChallengeFormModels) (*entities.ChallengeFormModels, error) - GetAllSubmitChallengeForm(page, perpage int) ([]entities.ChallengeFormModels, error) - GetSubmitChallengeFormByStatus(page, perpage int, status string) ([]entities.ChallengeFormModels, error) + GetAllSubmitChallengeForm(page, perpage int) ([]*entities.ChallengeFormModels, error) + GetSubmitChallengeFormByStatus(page, perpage int, status string) ([]*entities.ChallengeFormModels, error) GetTotalSubmitChallengeFormCount() (int64, error) - GetSubmitChallengeFormById(id uint64) (entities.ChallengeFormModels, error) - UpdateSubmitChallengeForm(id uint64, updatedStatus dto.UpdateChallengeFormStatusRequest) (entities.ChallengeFormModels, error) - GetSubmitChallengeFormByUserAndChallenge(userID uint64) ([]entities.ChallengeFormModels, error) + GetSubmitChallengeFormById(id uint64) (*entities.ChallengeFormModels, error) + UpdateSubmitChallengeForm(id uint64, updatedStatus dto.UpdateChallengeFormStatusRequest) (*entities.ChallengeFormModels, error) + GetSubmitChallengeFormByUserAndChallenge(userID uint64) ([]*entities.ChallengeFormModels, error) } type ServiceChallengeInterface interface { - GetAllChallenges(page, perPage int) ([]entities.ChallengeModels, int64, error) + GetAllChallenges(page, perPage int) ([]*entities.ChallengeModels, int64, error) CalculatePaginationValues(page int, totalItems int, perPage int) (int, int) GetNextPage(currentPage, totalPages int) int GetPrevPage(currentPage int) int - GetChallengeByTitle(page, perPage int, title string) ([]entities.ChallengeModels, int64, error) - CreateChallenge(newData entities.ChallengeModels) (entities.ChallengeModels, error) - UpdateChallenge(id uint64, updatedChallenge entities.ChallengeModels) (entities.ChallengeModels, error) - GetChallengeById(id uint64) (entities.ChallengeModels, error) + GetChallengeByTitle(page, perPage int, title string) ([]*entities.ChallengeModels, int64, error) + CreateChallenge(newData *entities.ChallengeModels) (*entities.ChallengeModels, error) + UpdateChallenge(id uint64, updatedChallenge *entities.ChallengeModels) (*entities.ChallengeModels, error) + GetChallengeById(id uint64) (*entities.ChallengeModels, error) DeleteChallenge(id uint64) error - - //partisipan CreateSubmitChallengeForm(form *entities.ChallengeFormModels) (*entities.ChallengeFormModels, error) - GetAllSubmitChallengeForm(page, perPage int) ([]entities.ChallengeFormModels, int64, error) - GetSubmitChallengeFormByStatus(page, perPage int, status string) ([]entities.ChallengeFormModels, int64, error) - GetSubmitChallengeFormById(id uint64) (entities.ChallengeFormModels, error) - UpdateSubmitChallengeForm(id uint64, updatedStatus dto.UpdateChallengeFormStatusRequest) (entities.ChallengeFormModels, error) + GetAllSubmitChallengeForm(page, perPage int) ([]*entities.ChallengeFormModels, int64, error) + GetSubmitChallengeFormByStatus(page, perPage int, status string) ([]*entities.ChallengeFormModels, int64, error) + GetSubmitChallengeFormById(id uint64) (*entities.ChallengeFormModels, error) + UpdateSubmitChallengeForm(id uint64, updatedStatus dto.UpdateChallengeFormStatusRequest) (*entities.ChallengeFormModels, error) } type HandlerChallengeInterface interface { @@ -51,8 +47,6 @@ type HandlerChallengeInterface interface { UpdateChallenge() echo.HandlerFunc DeleteChallengeById() echo.HandlerFunc GetChallengeById() echo.HandlerFunc - - //partisipan CreateSubmitChallengeForm() echo.HandlerFunc GetAllSubmitChallengeForm() echo.HandlerFunc UpdateSubmitChallengeForm() echo.HandlerFunc diff --git a/module/feature/challenge/repository/repository.go b/module/feature/challenge/repository/repository.go index 567f20a..c65be54 100644 --- a/module/feature/challenge/repository/repository.go +++ b/module/feature/challenge/repository/repository.go @@ -19,8 +19,8 @@ func NewChallengeRepository(db *gorm.DB) challenge.RepositoryChallengeInterface } } -func (r *ChallengeRepository) FindAll(page, perpage int) ([]entities.ChallengeModels, error) { - var challenge []entities.ChallengeModels +func (r *ChallengeRepository) FindAll(page, perpage int) ([]*entities.ChallengeModels, error) { + var challenge []*entities.ChallengeModels offset := (page - 1) * perpage err := r.db.Offset(offset).Limit(perpage).Find(&challenge).Error if err != nil { @@ -35,8 +35,8 @@ func (r *ChallengeRepository) GetTotalChallengeCount() (int64, error) { return count, err } -func (r *ChallengeRepository) FindByTitle(page, perpage int, title string) ([]entities.ChallengeModels, error) { - var challenge []entities.ChallengeModels +func (r *ChallengeRepository) FindByTitle(page, perpage int, title string) ([]*entities.ChallengeModels, error) { + var challenge []*entities.ChallengeModels offset := (page - 1) * perpage err := r.db.Offset(offset).Limit(perpage).Where("title LIKE?", "%"+title+"%").Find(&challenge).Error if err != nil { @@ -51,7 +51,7 @@ func (r *ChallengeRepository) GetTotalChallengeCountByTitle(title string) (int64 return count, err } -func (r *ChallengeRepository) CreateChallenge(newData entities.ChallengeModels) (entities.ChallengeModels, error) { +func (r *ChallengeRepository) CreateChallenge(newData *entities.ChallengeModels) (*entities.ChallengeModels, error) { if err := r.db.Create(&newData).Error; err != nil { return newData, err } @@ -59,8 +59,8 @@ func (r *ChallengeRepository) CreateChallenge(newData entities.ChallengeModels) return newData, nil } -func (r *ChallengeRepository) GetChallengeById(id uint64) (entities.ChallengeModels, error) { - var challenge = entities.ChallengeModels{} +func (r *ChallengeRepository) GetChallengeById(id uint64) (*entities.ChallengeModels, error) { + var challenge = &entities.ChallengeModels{} if err := r.db.Where("id = ? AND deleted_at IS NULL", id).First(&challenge).Error; err != nil { return challenge, err } @@ -68,9 +68,9 @@ func (r *ChallengeRepository) GetChallengeById(id uint64) (entities.ChallengeMod return challenge, nil } -func (r *ChallengeRepository) UpdateChallenge(challengeID uint64, updatedChallenge entities.ChallengeModels) (entities.ChallengeModels, error) { +func (r *ChallengeRepository) UpdateChallenge(challengeID uint64, updatedChallenge *entities.ChallengeModels) (*entities.ChallengeModels, error) { if err := r.db.Model(&entities.ChallengeModels{}).Where("id = ?", challengeID).Updates(updatedChallenge).Error; err != nil { - return entities.ChallengeModels{}, err + return &entities.ChallengeModels{}, err } return updatedChallenge, nil @@ -100,8 +100,8 @@ func (r *ChallengeRepository) CreateSubmitChallengeForm(form *entities.Challenge return form, nil } -func (r *ChallengeRepository) GetAllSubmitChallengeForm(page, perpage int) ([]entities.ChallengeFormModels, error) { - var participants []entities.ChallengeFormModels +func (r *ChallengeRepository) GetAllSubmitChallengeForm(page, perpage int) ([]*entities.ChallengeFormModels, error) { + var participants []*entities.ChallengeFormModels offset := (page - 1) * perpage err := r.db.Offset(offset).Limit(perpage).Find(&participants).Error if err != nil { @@ -110,8 +110,8 @@ func (r *ChallengeRepository) GetAllSubmitChallengeForm(page, perpage int) ([]en return participants, nil } -func (r *ChallengeRepository) GetSubmitChallengeFormByStatus(page, perpage int, status string) ([]entities.ChallengeFormModels, error) { - var participants []entities.ChallengeFormModels +func (r *ChallengeRepository) GetSubmitChallengeFormByStatus(page, perpage int, status string) ([]*entities.ChallengeFormModels, error) { + var participants []*entities.ChallengeFormModels offset := (page - 1) * perpage err := r.db.Where("status = ?", status).Offset(offset).Limit(perpage).Find(&participants).Error if err != nil { @@ -126,8 +126,8 @@ func (r *ChallengeRepository) GetTotalSubmitChallengeFormCount() (int64, error) return count, err } -func (r *ChallengeRepository) GetSubmitChallengeFormById(id uint64) (entities.ChallengeFormModels, error) { - var participant = entities.ChallengeFormModels{} +func (r *ChallengeRepository) GetSubmitChallengeFormById(id uint64) (*entities.ChallengeFormModels, error) { + var participant = &entities.ChallengeFormModels{} if err := r.db.Where("id = ? AND deleted_at IS NULL", id).First(&participant).Error; err != nil { return participant, err } @@ -135,16 +135,16 @@ func (r *ChallengeRepository) GetSubmitChallengeFormById(id uint64) (entities.Ch return participant, nil } -func (r *ChallengeRepository) UpdateSubmitChallengeForm(id uint64, updatedStatus dto.UpdateChallengeFormStatusRequest) (entities.ChallengeFormModels, error) { - var participant entities.ChallengeFormModels +func (r *ChallengeRepository) UpdateSubmitChallengeForm(id uint64, updatedStatus dto.UpdateChallengeFormStatusRequest) (*entities.ChallengeFormModels, error) { + var participant *entities.ChallengeFormModels if err := r.db.Model(&entities.ChallengeFormModels{}).Where("id = ? AND deleted_at IS NULL", id).Updates(updatedStatus).Error; err != nil { return participant, err } return participant, nil } -func (r *ChallengeRepository) GetSubmitChallengeFormByUserAndChallenge(userID uint64) ([]entities.ChallengeFormModels, error) { - var submissions []entities.ChallengeFormModels +func (r *ChallengeRepository) GetSubmitChallengeFormByUserAndChallenge(userID uint64) ([]*entities.ChallengeFormModels, error) { + var submissions []*entities.ChallengeFormModels err := r.db.Where("user_id = ?", userID).Find(&submissions).Error if err != nil { return nil, err diff --git a/module/feature/challenge/service/service.go b/module/feature/challenge/service/service.go index 1b30d74..ea03109 100644 --- a/module/feature/challenge/service/service.go +++ b/module/feature/challenge/service/service.go @@ -23,15 +23,15 @@ func NewChallengeService(repo challenge.RepositoryChallengeInterface, userServic } } -func (s *ChallengeService) GetAllChallenges(page, perPage int) ([]entities.ChallengeModels, int64, error) { +func (s *ChallengeService) GetAllChallenges(page, perPage int) ([]*entities.ChallengeModels, int64, error) { challenge, err := s.repo.FindAll(page, perPage) if err != nil { - return challenge, 0, err + return nil, 0, err } totalItems, err := s.repo.GetTotalChallengeCount() if err != nil { - return challenge, 0, err + return nil, 0, err } return challenge, totalItems, nil @@ -66,22 +66,22 @@ func (s *ChallengeService) GetPrevPage(currentPage int) int { return 1 } -func (s *ChallengeService) GetChallengeByTitle(page, perPage int, title string) ([]entities.ChallengeModels, int64, error) { +func (s *ChallengeService) GetChallengeByTitle(page, perPage int, title string) ([]*entities.ChallengeModels, int64, error) { challenge, err := s.repo.FindByTitle(page, perPage, title) if err != nil { - return challenge, 0, err + return nil, 0, err } totalItems, err := s.repo.GetTotalChallengeCountByTitle(title) if err != nil { - return challenge, 0, err + return nil, 0, err } return challenge, totalItems, nil } -func (s *ChallengeService) CreateChallenge(newData entities.ChallengeModels) (entities.ChallengeModels, error) { - newChallenge := entities.ChallengeModels{ +func (s *ChallengeService) CreateChallenge(newData *entities.ChallengeModels) (*entities.ChallengeModels, error) { + newChallenge := &entities.ChallengeModels{ Title: newData.Title, Photo: newData.Photo, StartDate: newData.StartDate, @@ -104,7 +104,7 @@ func (s *ChallengeService) CreateChallenge(newData entities.ChallengeModels) (en return result, nil } -func (s *ChallengeService) GetChallengeById(id uint64) (entities.ChallengeModels, error) { +func (s *ChallengeService) GetChallengeById(id uint64) (*entities.ChallengeModels, error) { result, err := s.repo.GetChallengeById(id) if err != nil { return result, errors.New("challenge tidak ditemukan") @@ -131,10 +131,10 @@ func updateIfNotZeroUint64(target *uint64, value uint64) { } } -func (s *ChallengeService) UpdateChallenge(challengeID uint64, updateData entities.ChallengeModels) (entities.ChallengeModels, error) { +func (s *ChallengeService) UpdateChallenge(challengeID uint64, updateData *entities.ChallengeModels) (*entities.ChallengeModels, error) { existingChallenge, err := s.repo.GetChallengeById(challengeID) if err != nil { - return entities.ChallengeModels{}, err + return &entities.ChallengeModels{}, err } updateIfNotEmpty(&existingChallenge.Title, updateData.Title) @@ -146,16 +146,16 @@ func (s *ChallengeService) UpdateChallenge(challengeID uint64, updateData entiti currentTime := time.Now() if currentTime.After(existingChallenge.EndDate) { - existingChallenge.Status = "Berakhir" + existingChallenge.Status = "Kadaluwarsa" } else { - existingChallenge.Status = "Berlangsung" + existingChallenge.Status = "Belum Kadaluwarsa" } updateIfNotEmpty(&existingChallenge.Status, updateData.Status) updatedChallenge, err := s.repo.UpdateChallenge(challengeID, existingChallenge) if err != nil { - return entities.ChallengeModels{}, err + return &entities.ChallengeModels{}, err } return updatedChallenge, nil @@ -211,38 +211,38 @@ func (s *ChallengeService) CreateSubmitChallengeForm(form *entities.ChallengeFor return nil, errors.New("Tantangan sudah kadaluwarsa, tidak dapat submit") } -func (s *ChallengeService) GetAllSubmitChallengeForm(page, perPage int) ([]entities.ChallengeFormModels, int64, error) { +func (s *ChallengeService) GetAllSubmitChallengeForm(page, perPage int) ([]*entities.ChallengeFormModels, int64, error) { challenge, err := s.repo.GetAllSubmitChallengeForm(page, perPage) if err != nil { - return challenge, 0, err + return nil, 0, err } totalItems, err := s.repo.GetTotalSubmitChallengeFormCount() if err != nil { - return challenge, 0, err + return nil, 0, err } return challenge, totalItems, nil } -func (s *ChallengeService) GetSubmitChallengeFormByStatus(page, perPage int, status string) ([]entities.ChallengeFormModels, int64, error) { +func (s *ChallengeService) GetSubmitChallengeFormByStatus(page, perPage int, status string) ([]*entities.ChallengeFormModels, int64, error) { challenge, err := s.repo.GetSubmitChallengeFormByStatus(page, perPage, status) if err != nil { - return challenge, 0, err + return nil, 0, err } totalItems, err := s.repo.GetTotalSubmitChallengeFormCount() if err != nil { - return challenge, 0, err + return nil, 0, err } return challenge, totalItems, nil } -func (s *ChallengeService) UpdateSubmitChallengeForm(id uint64, updatedData dto.UpdateChallengeFormStatusRequest) (entities.ChallengeFormModels, error) { +func (s *ChallengeService) UpdateSubmitChallengeForm(id uint64, updatedData dto.UpdateChallengeFormStatusRequest) (*entities.ChallengeFormModels, error) { form, err := s.repo.GetSubmitChallengeFormById(id) if err != nil { - return entities.ChallengeFormModels{}, errors.New("Form tidak ditemukan") + return &entities.ChallengeFormModels{}, errors.New("Form tidak ditemukan") } userID := form.UserID @@ -251,15 +251,28 @@ func (s *ChallengeService) UpdateSubmitChallengeForm(id uint64, updatedData dto. return form, errors.New("Gagal mendapatkan data user") } + var changeTotalChallenge uint64 + switch { case form.Status == "valid" && updatedData.Status == "tidak valid": user.Exp -= form.Exp + changeTotalChallenge-- case form.Status == "tidak valid" && updatedData.Status == "valid": user.Exp += form.Exp + changeTotalChallenge++ case form.Status == "menunggu validasi" && updatedData.Status == "valid": user.Exp += form.Exp + changeTotalChallenge++ case form.Status == "valid" && updatedData.Status == "menunggu validasi": user.Exp -= form.Exp + changeTotalChallenge-- + } + + user.TotalChallenge += changeTotalChallenge + + _, err = s.userService.UpdateUserChallengeFollow(userID, user.TotalChallenge) + if err != nil { + return form, errors.New("Gagal menyimpan perubahan total challenge user ke database") } _, err = s.userService.UpdateUserExp(userID, user.Exp) @@ -275,7 +288,7 @@ func (s *ChallengeService) UpdateSubmitChallengeForm(id uint64, updatedData dto. return result, nil } -func (s *ChallengeService) GetSubmitChallengeFormById(id uint64) (entities.ChallengeFormModels, error) { +func (s *ChallengeService) GetSubmitChallengeFormById(id uint64) (*entities.ChallengeFormModels, error) { result, err := s.repo.GetSubmitChallengeFormById(id) if err != nil { return result, errors.New("Form tidak ditemukan") diff --git a/module/feature/users/dto/response.go b/module/feature/users/dto/response.go index b249e0b..de8a73a 100644 --- a/module/feature/users/dto/response.go +++ b/module/feature/users/dto/response.go @@ -5,17 +5,18 @@ import ( ) type UserFormatter struct { - ID uint64 `json:"id"` - Email string `json:"email"` - Role string `json:"role"` - Name string `json:"name"` - Phone string `json:"phone"` - PhotoProfile string `json:"photo_profile"` - TotalGram uint64 `json:"total_gram"` - IsVerified bool `json:"is_verified"` - Level string `json:"level"` - Exp uint64 `json:"exp"` - Addresses []AddressFormatter `json:"addresses"` + ID uint64 `json:"id"` + Email string `json:"email"` + Role string `json:"role"` + Name string `json:"name"` + Phone string `json:"phone"` + PhotoProfile string `json:"photo_profile"` + TotalGram uint64 `json:"total_gram"` + TotalChallenge uint64 `json:"total_challenge"` + IsVerified bool `json:"is_verified"` + Level string `json:"level"` + Exp uint64 `json:"exp"` + Addresses []AddressFormatter `json:"addresses"` } type AddressFormatter struct { @@ -38,6 +39,7 @@ func FormatUser(user *entities.UserModels) *UserFormatter { userFormatter.Phone = user.Phone userFormatter.PhotoProfile = user.PhotoProfile userFormatter.TotalGram = user.TotalGram + userFormatter.TotalChallenge = user.TotalChallenge userFormatter.IsVerified = user.IsVerified userFormatter.Level = user.Level userFormatter.Exp = user.Exp diff --git a/module/feature/users/interface.go b/module/feature/users/interface.go index 7310e64..cf5a2b1 100644 --- a/module/feature/users/interface.go +++ b/module/feature/users/interface.go @@ -18,6 +18,7 @@ type RepositoryUserInterface interface { EditProfile(userID uint64, updatedData dto.EditProfileRequest) (*entities.UserModels, error) DeleteAccount(userID uint64) error UpdateUserExp(userID uint64, exp uint64) (*entities.UserModels, error) + UpdateUserChallengeFollow(userID uint64, totalChallenge uint64) (*entities.UserModels, error) } type ServiceUserInterface interface { @@ -33,6 +34,7 @@ type ServiceUserInterface interface { EditProfile(userID uint64, updatedData dto.EditProfileRequest) (*entities.UserModels, error) DeleteAccount(userID uint64) error UpdateUserExp(userID uint64, exp uint64) (*entities.UserModels, error) + UpdateUserChallengeFollow(userID uint64, totalChallenge uint64) (*entities.UserModels, error) } type HandlerUserInterface interface { diff --git a/module/feature/users/repository/respository.go b/module/feature/users/repository/respository.go index 8240273..d02e96b 100644 --- a/module/feature/users/repository/respository.go +++ b/module/feature/users/repository/respository.go @@ -130,3 +130,12 @@ func (r *UserRepository) UpdateUserExp(userID uint64, exp uint64) (*entities.Use return user, nil } + +func (r *UserRepository) UpdateUserChallengeFollow(userID uint64, totalChallenge uint64) (*entities.UserModels, error) { + user := &entities.UserModels{} + if err := r.db.Model(user).Where("id = ?", userID).Update("total_challenge", totalChallenge).Error; err != nil { + return nil, err + } + + return user, nil +} diff --git a/module/feature/users/service/service.go b/module/feature/users/service/service.go index 35a4be0..21f53e3 100644 --- a/module/feature/users/service/service.go +++ b/module/feature/users/service/service.go @@ -165,3 +165,12 @@ func (s *UserService) UpdateUserExp(userID uint64, exp uint64) (*entities.UserMo return user, nil } + +func (s *UserService) UpdateUserChallengeFollow(userID uint64, totalChallenge uint64) (*entities.UserModels, error) { + user, err := s.repo.UpdateUserChallengeFollow(userID, totalChallenge) + if err != nil { + return nil, err + } + + return user, nil +} diff --git a/routes/routes.go b/routes/routes.go index fa06b36..b4b750f 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -76,7 +76,6 @@ func RouteChallenge(e *echo.Echo, h challenge.HandlerChallengeInterface, jwtServ challengesGroup.PUT("/:id", h.UpdateChallenge(), middlewares.AuthMiddleware(jwtService, userService)) challengesGroup.DELETE("/:id", h.DeleteChallengeById(), middlewares.AuthMiddleware(jwtService, userService)) challengesGroup.GET("/:id", h.GetChallengeById()) - challengesGroup.POST("/submit", h.CreateSubmitChallengeForm(), middlewares.AuthMiddleware(jwtService, userService)) challengesGroup.PUT("/participants/status/:id", h.UpdateSubmitChallengeForm(), middlewares.AuthMiddleware(jwtService, userService)) challengesGroup.GET("/participants", h.GetAllSubmitChallengeForm())