Skip to content

Commit

Permalink
update program untuk sort /filter
Browse files Browse the repository at this point in the history
  • Loading branch information
rezapace committed Nov 29, 2023
1 parent afa3460 commit c199c83
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ CREATE TABLE IF NOT EXISTS "public"."tickets" (
Status TEXT DEFAULT 'available',
Quota INT,
Category TEXT,
Tersisa INT,
Created_At TIMESTAMP,
Updated_At TIMESTAMP,
Deleted_At TIMESTAMP
Expand Down
5 changes: 3 additions & 2 deletions entity/tiket.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ type Ticket struct {
Status string `json:"-"` // e.g., 'available', 'sold out'
Quota int64 `json:"-"`
Category string // e.g., 'music', 'sport', 'conference'
// Limit int64 `json:"limit"` // e.g., 1000, 5000, 10000
Tersisa int64 `json:"-"` // e.g., 1000, 5000, 10000
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
DeletedAt time.Time `json:"-"`
}

func NewTicket(image, location, date, title, description, category string, price, quota int64) *Ticket {
func NewTicket(image, location, date, title, description, category string, price, quota, tersisa int64) *Ticket {
return &Ticket{
Image: image,
Location: location,
Expand All @@ -31,6 +31,7 @@ func NewTicket(image, location, date, title, description, category string, price
Category: category,
Price: price,
Quota: quota,
Tersisa: tersisa,
}
}

Expand Down
76 changes: 20 additions & 56 deletions internal/http/handler/ticket_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func (h *TicketHandler) CreateTicket(c echo.Context) error {
Date time.Time `json:"date"`
Price float64 `json:"price"`
Quota int `json:"quota"`
Tersisa int `json:"tersisa"`
Category string `json:"category"`
}

Expand All @@ -63,6 +64,7 @@ func (h *TicketHandler) CreateTicket(c echo.Context) error {
Date: dateStr, // Assign the formatted date string
Price: int64(input.Price),
Quota: int64(input.Quota),
Tersisa: int64(input.Tersisa),
Category: input.Category,
CreatedAt: time.Now(),
}
Expand Down Expand Up @@ -289,74 +291,39 @@ func (h *TicketHandler) FilterTicketByPrice(c echo.Context) error {
})
}

// filter ticket dari yang paling banyak dibeli
// INI MASIH ERORR PAS RUN TEST POSTMANT!!!!!!!!!!!!
func (h *TicketHandler) FilterTicketByMostBought(c echo.Context) error {
// var input struct {
// Limit int64 `param:"limit" validate:"required"`
// }

// sortir tiket dari yang terbaru
func (h *TicketHandler) SortTicketByNewest(c echo.Context) error {
// Membaca parameter 'sort' dari URL
sortParam := c.QueryParam("sort")

// Memastikan bahwa parameter sort adalah 'terbaru'
if sortParam != "terfavorit" {
if sortParam != "terbaru" {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid sort parameter"})
}

// if err := c.Bind(&input); err != nil {
// return c.JSON(http.StatusBadRequest, validator.ValidatorErrors(err))
// }

// Memanggil service untuk mengurutkan tiket
tickets, err := h.ticketService.SortTicketByNewest(c.Request().Context())
if err != nil {
return c.JSON(http.StatusUnprocessableEntity, err)
}

var ticketResponses []map[string]interface{}
for _, ticket := range tickets {
ticketResponses = append(ticketResponses, map[string]interface{}{
"id": ticket.ID,
"title": ticket.Title,
"description": ticket.Description,
"image": ticket.Image,
"location": ticket.Location,
"date": ticket.Date,
"price": ticket.Price,
"quota": ticket.Quota,
"category": ticket.Category,
"created": ticket.CreatedAt,
})
}

return c.JSON(http.StatusOK, map[string]interface{}{
"data": ticketResponses,
"data": tickets,
})
}

// tickets, err := h.ticketService.FilterTicketByMostBought(c.Request().Context(), input.Limit)
// if err != nil {
// return c.JSON(http.StatusUnprocessableEntity, err)
// }

// return c.JSON(http.StatusOK, map[string]interface{}{
// "data": tickets,
// })

// sortir tiket dari yang terbaru
// INI MASIH ERORR PAS RUN TEST POSTMANT!!!!!!!!!!!!
func (h *TicketHandler) SortTicketByNewest(c echo.Context) error {
// sortir dari yang termahal
func (h *TicketHandler) SortTicketByMostExpensive(c echo.Context) error {
// Membaca parameter 'sort' dari URL
sortParam := c.QueryParam("sort")

// Memastikan bahwa parameter sort adalah 'terbaru'
if sortParam != "terbaru" {
// Memastikan bahwa parameter sort adalah 'termurah'
if sortParam != "termahal" {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid sort parameter"})
}

// Memanggil service untuk mengurutkan tiket
tickets, err := h.ticketService.SortTicketByNewest(c.Request().Context())
// Memanggil service untuk mengurutkan tiket dari yang termurah
tickets, err := h.ticketService.SortTicketByMostExpensive(c.Request().Context())
if err != nil {
return c.JSON(http.StatusUnprocessableEntity, err)
}
Expand All @@ -366,10 +333,8 @@ func (h *TicketHandler) SortTicketByNewest(c echo.Context) error {
})
}

// sortir tiket dari yang termurah
// ERROR DI LOGIKA NYA KAYA NYA.
// ticket yang paling banyak dibeli
func (h *TicketHandler) SortTicketByCheapest(c echo.Context) error {
// Membaca parameter 'sort' dari URL
sortParam := c.QueryParam("sort")

// Memastikan bahwa parameter sort adalah 'termurah'
Expand All @@ -388,23 +353,22 @@ func (h *TicketHandler) SortTicketByCheapest(c echo.Context) error {
})
}

// sortir dari yang termahal
func (h *TicketHandler) SortTicketByMostExpensive(c echo.Context) error {
// Membaca parameter 'sort' dari URL
// ticket yang paling banyak dibeli
func (h *TicketHandler) SortTicketByMostBought(c echo.Context) error {
sortParam := c.QueryParam("sort")

// Memastikan bahwa parameter sort adalah 'termurah'
if sortParam != "termahal" {
// Memastikan bahwa parameter sort adalah 'terbanyak'
if sortParam != "terbanyak" {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid sort parameter"})
}

// Memanggil service untuk mengurutkan tiket dari yang termurah
tickets, err := h.ticketService.SortTicketByMostExpensive(c.Request().Context())
// Memanggil service untuk mengurutkan tiket dari yang terbanyak
tickets, err := h.ticketService.SortTicketByMostBought(c.Request().Context())
if err != nil {
return c.JSON(http.StatusUnprocessableEntity, err)
}

return c.JSON(http.StatusOK, map[string]interface{}{
"data": tickets,
})
}
}
20 changes: 10 additions & 10 deletions internal/http/router/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,32 +220,32 @@ func PrivateRoutes(UserHandler *handler.UserHandler, TicketHandler *handler.Tick
Handler: TicketHandler.FilterTicketByPrice,
Role: allRoles,
},
// filter ticket by most bought
//sortir tiket dari yang terbaru
{
Method: echo.GET,
Path: "/ticket/most-bought",
Handler: TicketHandler.FilterTicketByMostBought,
Path: "/ticket/terbaru",
Handler: TicketHandler.SortTicketByNewest,
Role: allRoles,
},
//sortir tiket dari yang terbaru
//sortir tiket dari yang termahal
{
Method: echo.GET,
Path: "/tickets",
Handler: TicketHandler.SortTicketByNewest,
Path: "/ticket/most-expensive",
Handler: TicketHandler.SortTicketByMostExpensive,
Role: allRoles,
},
//sortir tiket dari yang termurah
{
Method: echo.GET,
Path: "/tickets/cheapest",
Path: "/ticket/cheapest",
Handler: TicketHandler.SortTicketByCheapest,
Role: allRoles,
},
//sortir tiket dari yang termahal
// filter ticket by most bought
{
Method: echo.GET,
Path: "/tickets/most-expensive",
Handler: TicketHandler.SortTicketByMostExpensive,
Path: "/ticket/most-bought",
Handler: TicketHandler.SortTicketByMostBought,
Role: allRoles,
},
}
Expand Down
20 changes: 10 additions & 10 deletions internal/repository/ticket_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,20 @@ func (r *TicketRepository) FilterTicketByPrice(ctx context.Context, min string,
return tickets, nil
}

// filter ticket dari yang paling banyak dibeli
func (r *TicketRepository) FilterTicketByMostBought(ctx context.Context) ([]*entity.Ticket, error) {
// sortir tiket dari yang terbaru
func (r *TicketRepository) SortTicketByNewest(ctx context.Context) ([]*entity.Ticket, error) {
tickets := make([]*entity.Ticket, 0)
result := r.db.WithContext(ctx).Order("quota DESC").Find(&tickets)
result := r.db.WithContext(ctx).Order("created_at DESC").Find(&tickets)
if result.Error != nil {
return nil, result.Error
}
return tickets, nil
}

// sortir tiket dari yang terbaru
func (r *TicketRepository) SortTicketByNewest(ctx context.Context) ([]*entity.Ticket, error) {
// sortir dari yang termahal
func (r *TicketRepository) SortTicketByMostExpensive(ctx context.Context) ([]*entity.Ticket, error) {
tickets := make([]*entity.Ticket, 0)
result := r.db.WithContext(ctx).Order("created_at DESC").Find(&tickets)
result := r.db.WithContext(ctx).Order("price DESC").Find(&tickets)
if result.Error != nil {
return nil, result.Error
}
Expand All @@ -141,17 +141,17 @@ func (r *TicketRepository) SortTicketByNewest(ctx context.Context) ([]*entity.Ti
// sortir tiket dari yang termurah
func (r *TicketRepository) SortTicketByCheapest(ctx context.Context) ([]*entity.Ticket, error) {
tickets := make([]*entity.Ticket, 0)
result := r.db.WithContext(ctx).Order("price ASC").Find(&tickets)
result := r.db.WithContext(ctx).Order("price DESC").Find(&tickets)
if result.Error != nil {
return nil, result.Error
}
return tickets, nil
}

// sortir dari yang termahal
func (r *TicketRepository) SortTicketByMostExpensive(ctx context.Context) ([]*entity.Ticket, error) {
// filter ticket dari yang paling banyak dibeli
func (r *TicketRepository) SortTicketByMostBought(ctx context.Context) ([]*entity.Ticket, error) {
tickets := make([]*entity.Ticket, 0)
result := r.db.WithContext(ctx).Order("price DESC").Find(&tickets)
result := r.db.WithContext(ctx).Order("Tersisa DESC").Find(&tickets)
if result.Error != nil {
return nil, result.Error
}
Expand Down
29 changes: 15 additions & 14 deletions internal/service/ticket_services.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ type TicketUseCase interface {
CreateTicket(ctx context.Context, ticket *entity.Ticket) error
GetTicket(ctx context.Context, id int64) (*entity.Ticket, error)
UpdateTicket(ctx context.Context, ticket *entity.Ticket) error
SearchTicket(ctx context.Context, search string) ([]*entity.Ticket, error)
DeleteTicket(ctx context.Context, id int64) error
SearchTicket(ctx context.Context, search string) ([]*entity.Ticket, error)
FilterTicket(ctx context.Context, location string) ([]*entity.Ticket, error)
FilterTicketByCategory(ctx context.Context, category string) ([]*entity.Ticket, error)
FilterTicketByRangeTime(ctx context.Context, start string, end string) ([]*entity.Ticket, error)
FilterTicketByPrice(ctx context.Context, min string, max string) ([]*entity.Ticket, error)
FilterTicketByMostBought(ctx context.Context) ([]*entity.Ticket, error)
SortTicketByNewest(ctx context.Context) ([]*entity.Ticket, error)
SortTicketByCheapest(ctx context.Context) ([]*entity.Ticket, error)
SortTicketByMostExpensive(ctx context.Context) ([]*entity.Ticket, error)
SortTicketByCheapest(ctx context.Context) ([]*entity.Ticket, error)
SortTicketByMostBought(ctx context.Context) ([]*entity.Ticket, error)

}

type TicketRepository interface {
Expand All @@ -35,10 +36,10 @@ type TicketRepository interface {
FilterTicketByCategory(ctx context.Context, category string) ([]*entity.Ticket, error)
FilterTicketByRangeTime(ctx context.Context, start string, end string) ([]*entity.Ticket, error)
FilterTicketByPrice(ctx context.Context, min string, max string) ([]*entity.Ticket, error)
FilterTicketByMostBought(ctx context.Context) ([]*entity.Ticket, error)
SortTicketByNewest(ctx context.Context) ([]*entity.Ticket, error)
SortTicketByCheapest(ctx context.Context) ([]*entity.Ticket, error)
SortTicketByMostExpensive(ctx context.Context) ([]*entity.Ticket, error)
SortTicketByCheapest(ctx context.Context) ([]*entity.Ticket, error)
SortTicketByMostBought(ctx context.Context) ([]*entity.Ticket, error)
}

// TicketService is responsible for ticket-related business logic.
Expand Down Expand Up @@ -96,22 +97,22 @@ func (s *TicketService) FilterTicketByPrice(ctx context.Context, min string, max
return s.Repository.FilterTicketByPrice(ctx, min, max)
}

// filter ticket by most bought
func (s *TicketService) FilterTicketByMostBought(ctx context.Context) ([]*entity.Ticket, error) {
return s.Repository.FilterTicketByMostBought(ctx)
}

// sortir tiket dari yang terbaru
func (s *TicketService) SortTicketByNewest(ctx context.Context) ([]*entity.Ticket, error) {
return s.Repository.SortTicketByNewest(ctx)
}

// sortir dari yang termahal
func (s *TicketService) SortTicketByMostExpensive(ctx context.Context) ([]*entity.Ticket, error) {
return s.Repository.SortTicketByMostExpensive(ctx)
}

// sortir tiket dari yang termurah
func (s *TicketService) SortTicketByCheapest(ctx context.Context) ([]*entity.Ticket, error) {
return s.Repository.SortTicketByCheapest(ctx)
}

// sortir dari yang termahal
func (s *TicketService) SortTicketByMostExpensive(ctx context.Context) ([]*entity.Ticket, error) {
return s.Repository.SortTicketByMostExpensive(ctx)
}
// filter ticket by most bought
func (s *TicketService) SortTicketByMostBought(ctx context.Context) ([]*entity.Ticket, error) {
return s.Repository.SortTicketByMostBought(ctx)
}

0 comments on commit c199c83

Please sign in to comment.