From c199c836d93fa135c04929bf213294406c77c025 Mon Sep 17 00:00:00 2001 From: Reza Hidayat Date: Thu, 30 Nov 2023 06:09:27 +0700 Subject: [PATCH] update program untuk sort /filter --- .../20231127065951_create_ticket_tabel.up.sql | 1 + entity/tiket.go | 5 +- internal/http/handler/ticket_handler.go | 76 +++++-------------- internal/http/router/routes.go | 20 ++--- internal/repository/ticket_repository.go | 20 ++--- internal/service/ticket_services.go | 29 +++---- 6 files changed, 59 insertions(+), 92 deletions(-) diff --git a/db/migration-golang/20231127065951_create_ticket_tabel.up.sql b/db/migration-golang/20231127065951_create_ticket_tabel.up.sql index 9f31011..230bbcc 100644 --- a/db/migration-golang/20231127065951_create_ticket_tabel.up.sql +++ b/db/migration-golang/20231127065951_create_ticket_tabel.up.sql @@ -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 diff --git a/entity/tiket.go b/entity/tiket.go index df4186e..19a821a 100644 --- a/entity/tiket.go +++ b/entity/tiket.go @@ -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, @@ -31,6 +31,7 @@ func NewTicket(image, location, date, title, description, category string, price Category: category, Price: price, Quota: quota, + Tersisa: tersisa, } } diff --git a/internal/http/handler/ticket_handler.go b/internal/http/handler/ticket_handler.go index 039a98f..7ef1a8a 100644 --- a/internal/http/handler/ticket_handler.go +++ b/internal/http/handler/ticket_handler.go @@ -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"` } @@ -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(), } @@ -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) } @@ -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' @@ -388,18 +353,17 @@ 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) } @@ -407,4 +371,4 @@ func (h *TicketHandler) SortTicketByMostExpensive(c echo.Context) error { return c.JSON(http.StatusOK, map[string]interface{}{ "data": tickets, }) -} +} \ No newline at end of file diff --git a/internal/http/router/routes.go b/internal/http/router/routes.go index 6d483a2..aa52807 100644 --- a/internal/http/router/routes.go +++ b/internal/http/router/routes.go @@ -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, }, } diff --git a/internal/repository/ticket_repository.go b/internal/repository/ticket_repository.go index c3def62..0ce67c1 100644 --- a/internal/repository/ticket_repository.go +++ b/internal/repository/ticket_repository.go @@ -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 } @@ -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 } diff --git a/internal/service/ticket_services.go b/internal/service/ticket_services.go index bfcda8c..db6b6a4 100644 --- a/internal/service/ticket_services.go +++ b/internal/service/ticket_services.go @@ -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 { @@ -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. @@ -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) +} \ No newline at end of file