Skip to content

Commit

Permalink
feature: users-total-challenge (#50)
Browse files Browse the repository at this point in the history
feature: users-total-challenge
  • Loading branch information
masnann authored Nov 27, 2023
2 parents 4ff5a22 + 6ee51fb commit 42eb0ec
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 113 deletions.
35 changes: 18 additions & 17 deletions module/entities/users_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 8 additions & 8 deletions module/feature/challenge/dto/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand Down
16 changes: 7 additions & 9 deletions module/feature/challenge/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -145,7 +144,7 @@ func (h *ChallengeHandler) UpdateChallenge() echo.HandlerFunc {
}
}

updatedChallenge := entities.ChallengeModels{
updatedChallenge := &entities.ChallengeModels{
ID: challengeID,
Title: updateRequest.Title,
Photo: uploadedURL,
Expand Down Expand Up @@ -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))
}
}

Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
44 changes: 19 additions & 25 deletions module/feature/challenge/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
38 changes: 19 additions & 19 deletions module/feature/challenge/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -51,26 +51,26 @@ 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
}

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
}

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
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -126,25 +126,25 @@ 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
}

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
Expand Down
Loading

0 comments on commit 42eb0ec

Please sign in to comment.