Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
rezapace committed Nov 29, 2023
2 parents 72fe52d + 1be00a1 commit afa3460
Show file tree
Hide file tree
Showing 10 changed files with 355 additions and 64 deletions.
27 changes: 13 additions & 14 deletions entity/tiket.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ import (
)

type Ticket struct {
ID int64 `json:"id"`
Image string `json:"image"`
Location string `json:"location"`
Date string // Format: YYYY-MM-DD
Title string `json:"title"`
Description string `json:"description"`
Price int64 `json:"price"`
Status string `json:"-"` // e.g., 'available', 'sold out'
Quota int64 `json:"-"`
ID int64 `json:"id"`
Image string `json:"image"`
Location string `json:"location"`
Date string // Format: YYYY-MM-DD
Title string `json:"title"`
Description string `json:"description"`
Price int64 `json:"price"`
Status string `json:"-"` // e.g., 'available', 'sold out'
Quota int64 `json:"-"`
Category string // e.g., 'music', 'sport', 'conference'
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
DeletedAt time.Time `json:"-"`
// Limit int64 `json:"limit"` // 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 {
Expand All @@ -30,7 +31,6 @@ func NewTicket(image, location, date, title, description, category string, price
Category: category,
Price: price,
Quota: quota,
CreatedAt: time.Now(),
}
}

Expand All @@ -45,6 +45,5 @@ func UpdateTicket(id int64, image, location, date, title, description, category
Category: category,
Price: price,
Quota: quota,
CreatedAt: time.Now(),
}
}
36 changes: 18 additions & 18 deletions entity/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,26 @@ import (
)

type User struct {
ID int64 `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
Number string `json:"number"`
Roles string `json:"roles"`
Saldo int64 `json:"saldo"`
Password string `json:"-"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt *time.Time `json:"deleted_at,omitempty"`
ID int64 `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
Number string `json:"number"`
Roles string `json:"roles"`
Saldo int64 `json:"saldo"`
Password string `json:"-"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt *time.Time `json:"deleted_at,omitempty"`
}

// Admin New User
func NewUser(name, email, number, password, roles string) *User {
func NewUser(name, email, number, password, roles string, saldo int64) *User {
return &User{
Name: name,
Email: email,
Number: number,
Roles: roles,
Saldo: saldo,
Password: password,
CreatedAt: time.Now(),
}
Expand All @@ -43,12 +44,11 @@ func UpdateUser(id int64, name, email, number, roles, password string) *User {
}

// Public Register
func Register(name, email, password, roles, number string) *User {
func Register(email, password, roles, number string) *User {
return &User{
Name: name,
Email: email,
Password: password,
Roles: roles,
Number: number,
Email: email,
Password: password,
Roles: roles,
Number: number,
}
}
}
21 changes: 10 additions & 11 deletions internal/http/handler/auth_hendler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (

type AuthHandler struct {
registrationService service.RegistrationUseCase // untuk regist
loginService service.LoginUseCase //untuk memanggil service yang ngelakuin pengecekan user.
tokenService service.TokenUsecase //untuk memanggil func akses token
loginService service.LoginUseCase //untuk memanggil service yang ngelakuin pengecekan user.
tokenService service.TokenUsecase //untuk memanggil func akses token
}

// ini func untuk type AuthHandler
Expand All @@ -23,8 +23,8 @@ func NewAuthHandler(
) *AuthHandler {
return &AuthHandler{
registrationService: registartionService,
loginService: loginService,
tokenService: tokenService,
loginService: loginService,
tokenService: tokenService,
}
}

Expand Down Expand Up @@ -62,19 +62,18 @@ func (h *AuthHandler) Login(ctx echo.Context) error {
func (h *AuthHandler) Registration(ctx echo.Context) error {
//pengecekan request
var input struct {
Name string `json:"name" validate:"required"`
Email string `json:"email" validate:"required,email"`
Password string `json:"password" validate:"required,min=8"`
Roles string `json:"roles" validate:"required"`
Number string `json:"number" validate:"required,min=11,max=13"`
Email string `json:"email" validate:"required,email"`
Password string `json:"password" validate:"required,min=8"`
Roles string `json:"roles" validate:"required"`
Number string `json:"number" validate:"required,min=11,max=13"`
}

if err := ctx.Bind(&input); err != nil { // di cek pake validate buat masukin input
return ctx.JSON(http.StatusBadRequest, validator.ValidatorErrors(err))
}

//untuk manggil registration service di folder service
user := entity.Register(input.Name, input.Email, input.Password, input.Roles, input.Number)
user := entity.Register(input.Email, input.Password, input.Roles, input.Number)
err := h.registrationService.Registration(ctx.Request().Context(), user)
if err != nil {
return ctx.JSON(http.StatusUnprocessableEntity, err)
Expand All @@ -90,4 +89,4 @@ func (h *AuthHandler) Registration(ctx echo.Context) error {
"access_token": accessToken,
})

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

// Input validation
Expand All @@ -63,7 +63,7 @@ func (h *TicketHandler) CreateTicket(c echo.Context) error {
Date: dateStr, // Assign the formatted date string
Price: int64(input.Price),
Quota: int64(input.Quota),
Category: input.Category,
Category: input.Category,
CreatedAt: time.Now(),
}

Expand Down Expand Up @@ -122,7 +122,7 @@ func (h *TicketHandler) UpdateTicket(c echo.Context) error {
Date time.Time `json:"date"`
Price float64 `json:"price"`
Quota int `json:"quota"`
Category string `json:"category"`
Category string `json:"category"`
}

if err := c.Bind(&input); err != nil {
Expand All @@ -142,7 +142,7 @@ func (h *TicketHandler) UpdateTicket(c echo.Context) error {
Date: dateStr, // Assign the formatted date string
Price: int64(input.Price), // Convert Price to int64 if needed
Quota: int64(input.Quota), // Convert Quota to int64 if needed
Category: input.Category,
Category: input.Category,
}

err := h.ticketService.UpdateTicket(c.Request().Context(), &ticket)
Expand All @@ -152,7 +152,18 @@ func (h *TicketHandler) UpdateTicket(c echo.Context) error {

return c.JSON(http.StatusOK, map[string]interface{}{
"message": "Ticket updated successfully",
"ticket": ticket,
"data": 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,
"update": ticket.UpdatedAt,
},
})
}

Expand Down Expand Up @@ -276,4 +287,124 @@ func (h *TicketHandler) FilterTicketByPrice(c echo.Context) error {
return c.JSON(http.StatusOK, map[string]interface{}{
"data": tickets,
})
}
}

// 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"`
// }

// Membaca parameter 'sort' dari URL
sortParam := c.QueryParam("sort")

// Memastikan bahwa parameter sort adalah 'terbaru'
if sortParam != "terfavorit" {
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,
})
}

// 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 {
// Membaca parameter 'sort' dari URL
sortParam := c.QueryParam("sort")

// Memastikan bahwa parameter sort adalah 'terbaru'
if sortParam != "terbaru" {
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())
if err != nil {
return c.JSON(http.StatusUnprocessableEntity, err)
}

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

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

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

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

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

// 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 'termurah'
if sortParam != "termahal" {
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())
if err != nil {
return c.JSON(http.StatusUnprocessableEntity, err)
}

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

0 comments on commit afa3460

Please sign in to comment.