Skip to content

Commit

Permalink
Merge pull request #6 from capstone-kelompok-7/feature/vouch
Browse files Browse the repository at this point in the history
improvment: feature/voucher
  • Loading branch information
masnann authored Nov 8, 2023
2 parents 11e4342 + 9fc61f1 commit 0d16102
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 88 deletions.
18 changes: 9 additions & 9 deletions module/voucher/domain/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import (

type VoucherModels struct {
gorm.Model
ID uint64 `gorm:"column:id;type:BIGINT UNSIGNED;primaryKey" json:"id"`
Name string `gorm:"column:name;type:VARCHAR(255)" json:"name"`
Code string `gorm:"column:code;type:VARCHAR(255)" json:"code"`
Category string `gorm:"column:category;type:VARCHAR(255)" json:"category"`
Description string `gorm:"column:description;type:TEXT" json:"description"`
Discouunt int `gorm:"column:discount;type:INT" json:"discount"`
StartDate string `gorm:"column:start_date;type:DATETIME" json:"start_date"`
EndDate string `gorm:"column:end_date; type:DATETIME" json:"end_date"`
MinAmount float64 `gorm:"column:min_amount;type:DECIMAL(10, 2)" json:"min_amount"`
ID uint64 `gorm:"column:id;type:BIGINT UNSIGNED;primaryKey" json:"id" `
Name string `gorm:"column:name;type:VARCHAR(255)" json:"name" `
Code string `gorm:"column:code;type:VARCHAR(255);unique" json:"code" `
Category string `gorm:"column:category;type:VARCHAR(255)" json:"category" `
Description string `gorm:"column:description;type:TEXT" json:"description" `
Discouunt int `gorm:"column:discount;type:INT" json:"discount" `
StartDate string `gorm:"column:start_date;type:DATETIME" json:"start_date" `
EndDate string `gorm:"column:end_date; type:DATETIME" json:"end_date" `
MinAmount float64 `gorm:"column:min_amount;type:DECIMAL(10, 2)" json:"min_amount" `
}

func (VoucherModels) TableName() string {
Expand Down
13 changes: 13 additions & 0 deletions module/voucher/domain/request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package domain

type VoucherRequestModel struct {
ID uint64 `json:"id" validate:"required"`
Name string `json:"name" validate:"required"`
Code string `json:"code" validate:"required"`
Category string `json:"category" validate:"required"`
Description string `json:"description" validate:"required"`
Discouunt int `json:"discount" validate:"required"`
StartDate string `json:"start_date" validate:"required"`
EndDate string `json:"end_date" validate:"required"`
MinAmount float64 `json:"min_amount" validate:"required"`
}
44 changes: 36 additions & 8 deletions module/voucher/domain/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,66 @@ type VoucherModelsResponseAll struct {
Status string `json:"status"`
}

func VoucherResponseFormatter(voucher VoucherModels) VoucherModelsResponse {
func VoucherResponseFormatterCreate(voucher *VoucherModels) VoucherModelsResponse {
voucherFormatter := VoucherModelsResponse{}
voucherFormatter.ID = voucher.ID
voucherFormatter.Name = voucher.Name
voucherFormatter.Code = voucher.Code
voucherFormatter.Category = voucher.Category
voucherFormatter.Discouunt = voucher.Discouunt

sampleFormat := "2006-01-02"

var dateNow = time.Now()
dateNowFormat := dateNow.Format(sampleFormat)

noww, _ := time.Parse("2006-01-02", dateNowFormat)
endd, _ := time.Parse("2006-01-02", voucher.EndDate)

if noww.After(endd) {
voucherFormatter.Status = "kadaluarsa"
} else if noww.Before(endd) {
voucherFormatter.Status = "aktif"
}

voucherFormatter.StartDate = voucher.StartDate
voucherFormatter.EndDate = voucher.EndDate

return voucherFormatter
}
func VoucherResponseFormatter(voucher *VoucherModels) VoucherModelsResponse {
voucherFormatter := VoucherModelsResponse{}
voucherFormatter.ID = voucher.ID
voucherFormatter.Name = voucher.Name
voucherFormatter.Code = voucher.Code
voucherFormatter.Category = voucher.Category
voucherFormatter.Discouunt = voucher.Discouunt

sampleFormat := "2006-01-02"

var dateNow = time.Now()
dateNowFormat := dateNow.Format(sampleFormat)

// parsedstartdate, _ := time.Parse(time.RFC3339Nano, voucher.StartDate)
// formatstartdate := parsedstartdate.Format(sampleFormat)
// voucherFormatter.StartDate = formatstartdate
parsedStartDate, _ := time.Parse(time.RFC3339Nano, voucher.StartDate)
formatstartdate := parsedStartDate.Format("2006-01-02")
voucherFormatter.StartDate = formatstartdate

// parsedenddate, _ := time.Parse(time.RFC3339Nano, voucher.EndDate)
// formatenddate := parsedenddate.Format(sampleFormat)
// voucherFormatter.EndDate = formatenddate
parsedenddate, _ := time.Parse(time.RFC3339Nano, voucher.EndDate)
formatenddate := parsedenddate.Format(sampleFormat)
voucherFormatter.EndDate = formatenddate

noww, _ := time.Parse("2006-01-02", dateNowFormat)
endd, _ := time.Parse("2006-01-02", voucher.EndDate)
endd, _ := time.Parse("2006-01-02", formatenddate)

if noww.After(endd) {
voucherFormatter.Status = "kadaluarsa"
} else if noww.Before(endd) {
voucherFormatter.Status = "aktif"
}

voucherFormatter.StartDate = formatstartdate
voucherFormatter.EndDate = formatenddate

return voucherFormatter
}
func VoucherResponseFormatterAll(voucher VoucherModels) VoucherModelsResponseAll {
Expand Down
118 changes: 57 additions & 61 deletions module/voucher/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/capstone-kelompok-7/backend-disappear/module/voucher"
"github.com/capstone-kelompok-7/backend-disappear/module/voucher/domain"
"github.com/capstone-kelompok-7/backend-disappear/utils"
"github.com/capstone-kelompok-7/backend-disappear/utils/response"
"github.com/labstack/echo/v4"
)
Expand All @@ -22,73 +23,64 @@ func NewVoucherHandler(service voucher.ServiceVoucherInterface) voucher.HandlerV

func (h *VoucherHandler) CreateVoucher() echo.HandlerFunc {
return func(c echo.Context) error {
var input = domain.VoucherModels{}
c.Bind(&input)
var voucherRequest = new(domain.VoucherRequestModel)

if err := c.Bind(&voucherRequest); err != nil {
return response.SendErrorResponse(c, http.StatusBadRequest, "Format input yang Anda masukkan tidak sesuai.")
}

if input.Name == "" || input.Category == "" || input.Code == "" || input.Description == "" || input.Discouunt < 0 || input.EndDate == "" || input.StartDate == "" {
return c.JSON(http.StatusBadRequest, map[string]interface{}{
"message": "invalid input",
})
if err := utils.ValidateStruct(voucherRequest); err != nil {
return response.SendErrorResponse(c, http.StatusBadRequest, "Validasi gagal: "+err.Error())
}

result, err := h.service.CreateVoucher(input)
newVoucher := &domain.VoucherModels{
ID: voucherRequest.ID,
Name: voucherRequest.Name,
Code: voucherRequest.Code,
Category: voucherRequest.Category,
Description: voucherRequest.Description,
Discouunt: voucherRequest.Discouunt,
StartDate: voucherRequest.StartDate,
EndDate: voucherRequest.EndDate,
MinAmount: voucherRequest.MinAmount,
}

result, err := h.service.CreateVoucher(*newVoucher)
if err != nil {
c.Logger().Error("handler: failed create voucher:", err.Error())
return response.SendErrorResponse(c, http.StatusInternalServerError, "Internal Server Error")
return response.SendErrorResponse(c, http.StatusInternalServerError, "Internal Server Error, "+err.Error())
}

return c.JSON(http.StatusCreated, map[string]interface{}{
"message": "Voucher berhasil ditambahkan.",
"data": domain.VoucherResponseFormatter(*result),
})
return response.SendSuccessResponse(c, "Voucher berhasil ditambahkan", domain.VoucherResponseFormatterCreate(result))
}
}

func (h *VoucherHandler) GetAllVouchers() echo.HandlerFunc {
return func(c echo.Context) error {
page := c.QueryParam("page")
search := c.QueryParam("search")
pagee, err := strconv.Atoi(page)
if err != nil {
return response.SendErrorResponse(c, http.StatusInternalServerError, "Invalid page number")
}
limit := c.QueryParam("limit")
limitt, err := strconv.Atoi(limit)
if err != nil {
return response.SendErrorResponse(c, http.StatusInternalServerError, "Invalid limit number")
}
category := c.QueryParam("category")
pageconv, _ := strconv.Atoi(page)

if page == "" {
page = "1"
}
prevPage := pageconv - 1
nextPage := pageconv + 1

if limit == "" {
limit = "5"
}

prevPage := pagee - 1
nextPage := pagee + 1
allvoucher, _ := h.service.GetAllVouchersToCalculatePage()
var calculatePage = len(allvoucher) / 8

if prevPage < 1 {
prevPage = 1
}

result, err := h.service.GetAllVouchers(pagee, limitt, search)
if pageconv == 0 {
pageconv = 1
}

result, err := h.service.GetAllVouchers(pageconv, 8, category)
if err != nil {
c.Logger().Error("handler: failed create voucher:", err.Error())
return response.SendErrorResponse(c, http.StatusInternalServerError, "Internal Server Error")
return response.SendErrorResponse(c, http.StatusInternalServerError, "Internal Server Error, "+err.Error())
}

return c.JSON(http.StatusCreated, map[string]interface{}{
"message": "Berhasil.",
"data": domain.VoucherModelsFormatterAll(result),
"pagination": map[string]interface{}{
"current_page": pagee,
"toal_page": len(result),
"previous_page": prevPage,
"next_page": nextPage,
},
})
return response.Pagination(c, domain.VoucherModelsFormatterAll(result), pageconv, calculatePage, len(result), nextPage, prevPage, "Berhasil")
}
}

Expand All @@ -98,43 +90,47 @@ func (h *VoucherHandler) EditVoucherById() echo.HandlerFunc {
var voucherid = c.Param("voucher_id")
voucheridfix, _ := strconv.Atoi(voucherid)

c.Bind(&input)
if err := c.Bind(&input); err != nil {
return response.SendErrorResponse(c, http.StatusBadRequest, "Format input yang Anda masukkan tidak sesuai.")
}

if input.Name == "" || input.Category == "" || input.Code == "" || input.Description == "" || input.Discouunt < 0 || input.EndDate == "" || input.StartDate == "" {
return c.JSON(http.StatusBadRequest, map[string]interface{}{
"message": "invalid input",
})
if input.Name == "" || input.Code == "" || input.Category == "" || input.Description == "" || input.Discouunt < 0 || input.MinAmount < 0 || input.StartDate == "" || input.EndDate == "" {
return response.SendErrorResponse(c, http.StatusBadRequest, "Invalid Input")
}

input.ID = uint64(voucheridfix)
result, err := h.service.EditVoucherById(input)
if err != nil {
c.Logger().Error("handler: failed edit voucher:", err.Error())
return response.SendErrorResponse(c, http.StatusInternalServerError, "Internal Server Error")
return response.SendErrorResponse(c, http.StatusInternalServerError, "Internal Server Error, "+err.Error())
}

return c.JSON(http.StatusCreated, map[string]interface{}{
"message": "Voucher berhasil diperbarui.",
"data": domain.VoucherResponseFormatter(*result),
})

return response.SendSuccessResponse(c, "Voucher berhasil diperbarui", domain.VoucherResponseFormatterCreate(result))
}
}
func (h *VoucherHandler) DeleteVoucherById() echo.HandlerFunc {
return func(c echo.Context) error {

var voucherid = c.Param("voucher_id")
voucheridfix, _ := strconv.Atoi(voucherid)

result := h.service.DeleteVoucherById(voucheridfix)
if result != nil {
c.Logger().Error("handler: failed get voucher:", result.Error())
c.Logger().Error("handler: failed delete voucher:", result.Error())
return response.SendErrorResponse(c, http.StatusInternalServerError, "Internal Server Error")
}
return response.SendStatusOkResponse(c, "Voucher berhasil dihapus.")
}
}

return c.JSON(http.StatusCreated, map[string]interface{}{
"message": "Voucher berhasil dihapus.",
})
func (h *VoucherHandler) GetVoucherById() echo.HandlerFunc {
return func(c echo.Context) error {
voucherid := c.Param("voucher_id")
voucherconv, _ := strconv.Atoi(voucherid)

result, err := h.service.GetVoucherById(voucherconv)
if err != nil {
c.Logger().Error("handler: failed get voucher:", err.Error())
return response.SendErrorResponse(c, http.StatusInternalServerError, "Internal Server Error, "+err.Error())
}
return response.SendSuccessResponse(c, "Berhasil.", domain.VoucherResponseFormatter(result))
}
}
8 changes: 5 additions & 3 deletions module/voucher/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,24 @@ type RepositoryVoucherInterface interface {
CreateVoucher(newData voucher.VoucherModels) (*voucher.VoucherModels, error)
GetAllVouchers(page int, limit int, search string) ([]voucher.VoucherModels, error)
DeleteVoucherById(id int) error
// DeleteVoucherByName(name string) error
GetVoucherById(id int) (*voucher.VoucherModels, error)
EditVoucherById(data voucher.VoucherModels) (*voucher.VoucherModels, error)
GetAllVouchersToCalculatePage() ([]voucher.VoucherModels, error)
}

type ServiceVoucherInterface interface {
CreateVoucher(newData voucher.VoucherModels) (*voucher.VoucherModels, error)
GetAllVouchers(page int, limit int, search string) ([]voucher.VoucherModels, error)
DeleteVoucherById(id int) error
// DeleteVoucherByName(name string) error
GetVoucherById(id int) (*voucher.VoucherModels, error)
EditVoucherById(data voucher.VoucherModels) (*voucher.VoucherModels, error)
GetAllVouchersToCalculatePage() ([]voucher.VoucherModels, error)
}

type HandlerVoucherInterface interface {
CreateVoucher() echo.HandlerFunc
GetAllVouchers() echo.HandlerFunc
DeleteVoucherById() echo.HandlerFunc
// DeleteVoucherByName() echo.HandlerFunc
GetVoucherById() echo.HandlerFunc
EditVoucherById() echo.HandlerFunc
}
29 changes: 22 additions & 7 deletions module/voucher/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (r *VoucherRepository) GetAllVouchers(currentPage int, limit int, search st
var listVoucher = []domain.VoucherModels{}

if search != "" {
if err := r.db.Where("name LIKE ?", "%"+search+"%").Offset((currentPage - 1) * limit).Limit(limit).Find(&listVoucher).Error; err != nil {
if err := r.db.Where("category LIKE ?", "%"+search+"%").Offset((currentPage - 1) * limit).Limit(limit).Find(&listVoucher).Error; err != nil {
return nil, err
}
} else if search == "" {
Expand All @@ -39,13 +39,19 @@ func (r *VoucherRepository) GetAllVouchers(currentPage int, limit int, search st
return listVoucher, nil
}

// func (r *VoucherRepository) GetVoucherByName(name string) (*domain.VoucherModels, error) {
func (r *VoucherRepository) GetAllVouchersToCalculatePage() ([]domain.VoucherModels, error) {
var listVoucher = []domain.VoucherModels{}

// }
if err := r.db.Find(&listVoucher).Error; err != nil {
return nil, err
}

func (r *VoucherRepository) EditVoucherById(data domain.VoucherModels) (*domain.VoucherModels, error) {
return listVoucher, nil
}

if err := r.db.Model(&data).Where("id = ?", data.ID).Updates(map[string]interface{}{
func (r *VoucherRepository) EditVoucherById(data domain.VoucherModels) (*domain.VoucherModels, error) {
var voucher = domain.VoucherModels{}
if err := r.db.Model(&voucher).Where("id = ?", data.ID).Updates(map[string]interface{}{
"name": data.Name,
"code": data.Code,
"category": data.Category,
Expand All @@ -58,15 +64,24 @@ func (r *VoucherRepository) EditVoucherById(data domain.VoucherModels) (*domain.
return nil, err
}

return &data, nil
return &voucher, nil
}

func (r *VoucherRepository) DeleteVoucherById(id int) error {
var voucher = domain.VoucherModels{}

if err := r.db.Where("id = ?", id).Delete(&voucher).Error; err != nil {
if err := r.db.Unscoped().Where("id = ?", id).Delete(&voucher).Error; err != nil {
return nil
}
return nil

}

func (r *VoucherRepository) GetVoucherById(id int) (*domain.VoucherModels, error) {
var voucher = domain.VoucherModels{}
if err := r.db.Where("id = ?", id).First(&voucher).Error; err != nil {
return nil, err
}

return &voucher, nil
}
Loading

0 comments on commit 0d16102

Please sign in to comment.