Add User Partner and Product

This commit is contained in:
aditya.siregar
2024-06-03 14:40:50 +07:00
parent 67f1dbc850
commit 8a23e72230
46 changed files with 1366 additions and 213 deletions
+8 -7
View File
@@ -51,19 +51,20 @@ func (h *AuthHandler) AuthLogin(c *gin.Context) {
return
}
var branch *response.Branch
var partner *response.Partner
if authUser.RoleID != role.SuperAdmin {
branch = &response.Branch{
ID: authUser.BranchID,
Name: authUser.BranchName,
partner = &response.Partner{
ID: authUser.PartnerID,
Name: authUser.PartnerName,
Status: authUser.PartnerStatus,
}
}
resp := response.LoginResponse{
Token: authUser.Token,
Branch: branch,
Name: authUser.Name,
Token: authUser.Token,
Partner: partner,
Name: authUser.Name,
Role: response.Role{
ID: int64(authUser.RoleID),
Role: authUser.RoleName,
+1 -1
View File
@@ -51,7 +51,7 @@ func NewHandler(service services.Partner) *Handler {
func (h *Handler) Create(c *gin.Context) {
ctx := request.GetMyContext(c)
var req request.Partner
var req request.CreatePartnerRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.ErrorWrapper(c, errors.ErrorBadRequest)
return
@@ -246,9 +246,6 @@ func (h *Handler) toProductResponse(resp *entity.Product) response.Product {
Price: resp.Price,
Status: resp.Status,
Description: resp.Description,
Image: resp.Image,
BranchID: resp.BranchID,
StockQty: resp.StockQty,
CreatedAt: resp.CreatedAt.Format(time.RFC3339),
UpdatedAt: resp.CreatedAt.Format(time.RFC3339),
}
+299
View File
@@ -0,0 +1,299 @@
package site
import (
"furtuna-be/internal/common/errors"
"furtuna-be/internal/entity"
"furtuna-be/internal/handlers/request"
"furtuna-be/internal/handlers/response"
"furtuna-be/internal/services"
"net/http"
"strconv"
"time"
"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
)
type Handler struct {
service services.Site
}
func (h *Handler) Route(group *gin.RouterGroup, jwt gin.HandlerFunc) {
route := group.Group("/site")
route.POST("/", jwt, h.Create)
route.GET("/list", jwt, h.GetAll)
route.PUT("/:id", jwt, h.Update)
route.GET("/:id", jwt, h.GetByID)
route.DELETE("/:id", jwt, h.Delete)
}
func NewHandler(service services.Site) *Handler {
return &Handler{
service: service,
}
}
// Create handles the creation of a new Site.
// @Summary Create a new Site
// @Description Create a new Site based on the provided data.
// @Accept json
// @Produce json
// @Param Authorization header string true "JWT token"
// @Param req body request.Site true "New Site details"
// @Success 200 {object} response.BaseResponse{data=response.Site} "Site created successfully"
// @Failure 400 {object} response.BaseResponse{data=errors.Error} "Bad request"
// @Failure 401 {object} response.BaseResponse{data=errors.Error} "Unauthorized"
// @Router /api/v1/site [post]
// @Tags Site APIs
func (h *Handler) Create(c *gin.Context) {
ctx := request.GetMyContext(c)
var req request.Site
if err := c.ShouldBindJSON(&req); err != nil {
response.ErrorWrapper(c, errors.ErrorBadRequest)
return
}
if !ctx.IsSuperAdmin() {
req.PartnerID = ctx.GetPartnerID()
}
validate := validator.New()
if err := validate.Struct(req); err != nil {
response.ErrorWrapper(c, err)
return
}
res, err := h.service.Create(ctx, req.ToEntity(ctx.RequestedBy()))
if err != nil {
response.ErrorWrapper(c, err)
return
}
c.JSON(http.StatusOK, response.BaseResponse{
Success: true,
Status: http.StatusOK,
Data: h.toSiteResponse(res),
})
}
// Update handles the update of an existing Site.
// @Summary Update an existing Site
// @Description Update the details of an existing Site based on the provided ID.
// @Accept json
// @Produce json
// @Param Authorization header string true "JWT token"
// @Param id path int64 true "Site ID to update"
// @Param req body request.Site true "Updated Site details"
// @Success 200 {object} response.BaseResponse{data=response.Site} "Site updated successfully"
// @Failure 400 {object} response.BaseResponse{data=errors.Error} "Bad request"
// @Failure 401 {object} response.BaseResponse{data=errors.Error} "Unauthorized"
// @Router /api/v1/site/{id} [put]
// @Tags Site APIs
func (h *Handler) Update(c *gin.Context) {
ctx := request.GetMyContext(c)
id := c.Param("id")
SiteID, err := strconv.ParseInt(id, 10, 64)
if err != nil {
response.ErrorWrapper(c, errors.ErrorBadRequest)
return
}
var req request.Site
if err := c.ShouldBindJSON(&req); err != nil {
response.ErrorWrapper(c, errors.ErrorBadRequest)
return
}
validate := validator.New()
if err := validate.Struct(req); err != nil {
response.ErrorWrapper(c, err)
return
}
updatedSite, err := h.service.Update(ctx, SiteID, req.ToEntity(ctx.RequestedBy()))
if err != nil {
response.ErrorWrapper(c, err)
return
}
c.JSON(http.StatusOK, response.BaseResponse{
Success: true,
Status: http.StatusOK,
Data: h.toSiteResponse(updatedSite),
})
}
// GetAll retrieves a list of Sites.
// @Summary Get a list of Sites
// @Description Get a paginated list of Sites based on query parameters.
// @Accept json
// @Produce json
// @Param Authorization header string true "JWT token"
// @Param Limit query int false "Number of items to retrieve (default 10)"
// @Param Offset query int false "Offset for pagination (default 0)"
// @Success 200 {object} response.BaseResponse{data=response.SiteList} "List of Sites"
// @Failure 400 {object} response.BaseResponse{data=errors.Error} "Bad request"
// @Failure 401 {object} response.BaseResponse{data=errors.Error} "Unauthorized"
// @Router /api/v1/site/list [get]
// @Tags Site APIs
func (h *Handler) GetAll(c *gin.Context) {
var req request.SiteParam
if err := c.ShouldBindQuery(&req); err != nil {
response.ErrorWrapper(c, errors.ErrorBadRequest)
return
}
Sites, total, err := h.service.GetAll(c.Request.Context(), req.ToEntity())
if err != nil {
response.ErrorWrapper(c, err)
return
}
c.JSON(http.StatusOK, response.BaseResponse{
Success: true,
Status: http.StatusOK,
Data: h.toSiteResponseList(Sites, int64(total), req),
})
}
// Delete handles the deletion of a Site by ID.
// @Summary Delete a Site by ID
// @Description Delete a Site based on the provided ID.
// @Accept json
// @Produce json
// @Param Authorization header string true "JWT token"
// @Param id path int64 true "Site ID to delete"
// @Success 200 {object} response.BaseResponse "Site deleted successfully"
// @Failure 400 {object} response.BaseResponse{data=errors.Error} "Bad request"
// @Failure 401 {object} response.BaseResponse{data=errors.Error} "Unauthorized"
// @Router /api/v1/site/{id} [delete]
// @Tags Site APIs
func (h *Handler) Delete(c *gin.Context) {
ctx := request.GetMyContext(c)
id := c.Param("id")
// Parse the ID into a uint
SiteID, err := strconv.ParseInt(id, 10, 64)
if err != nil {
response.ErrorWrapper(c, errors.ErrorBadRequest)
return
}
err = h.service.Delete(ctx, SiteID)
if err != nil {
c.JSON(http.StatusInternalServerError, response.BaseResponse{
Success: false,
Status: http.StatusInternalServerError,
Message: err.Error(),
Data: nil,
})
return
}
c.JSON(http.StatusOK, response.BaseResponse{
Success: true,
Status: http.StatusOK,
Data: nil,
})
}
// GetByID retrieves details of a specific Site by ID.
// @Summary Get details of a Site by ID
// @Description Get details of a Site based on the provided ID.
// @Accept json
// @Produce json
// @Param Authorization header string true "JWT token"
// @Param id path int64 true "Site ID to retrieve"
// @Success 200 {object} response.BaseResponse{data=response.Site} "Site details"
// @Failure 400 {object} response.BaseResponse{data=errors.Error} "Bad request"
// @Failure 401 {object} response.BaseResponse{data=errors.Error} "Unauthorized"
// @Router /api/v1/site/{id} [get]
// @Tags Site APIs
func (h *Handler) GetByID(c *gin.Context) {
id := c.Param("id")
// Parse the ID into a uint
SiteID, err := strconv.ParseInt(id, 10, 64)
if err != nil {
response.ErrorWrapper(c, errors.ErrorBadRequest)
return
}
res, err := h.service.GetByID(c.Request.Context(), SiteID)
if err != nil {
c.JSON(http.StatusInternalServerError, response.BaseResponse{
Success: false,
Status: http.StatusInternalServerError,
Message: err.Error(),
Data: nil,
})
return
}
c.JSON(http.StatusOK, response.BaseResponse{
Success: true,
Status: http.StatusOK,
Data: h.toSiteResponse(res),
})
}
func (h *Handler) toSiteResponse(resp *entity.Site) response.Site {
return response.Site{
ID: &resp.ID,
Name: resp.Name,
PartnerID: resp.PartnerID,
Image: resp.Image,
Address: resp.Address,
LocationLink: resp.LocationLink,
Description: resp.Description,
Highlight: resp.Highlight,
ContactPerson: resp.ContactPerson,
TnC: resp.TnC,
AdditionalInfo: resp.AdditionalInfo,
Status: resp.Status,
IsSeasonTicket: resp.IsSeasonTicket,
IsDiscountActive: resp.IsDiscountActive,
CreatedAt: resp.CreatedAt.Format(time.RFC3339),
UpdatedAt: resp.UpdatedAt.Format(time.RFC3339),
}
}
func (h *Handler) toSiteResponseList(resp []*entity.Site, total int64, req request.SiteParam) response.SiteList {
var Sites []response.Site
for _, b := range resp {
Sites = append(Sites, h.toSiteResponse(b))
}
return response.SiteList{
Sites: Sites,
Total: total,
Limit: req.Limit,
Offset: req.Offset,
}
}
func (h *Handler) toProductResponseList(products []entity.Product) []response.Product {
var res []response.Product
for _, product := range products {
res = append(res, response.Product{
ID: product.ID,
PartnerID: product.PartnerID,
SiteID: product.SiteID,
Name: product.Name,
Type: product.Type,
Price: product.Price,
IsWeekendTicket: product.IsWeekendTicket,
IsSeasonTicket: product.IsSeasonTicket,
Status: product.Status,
Description: product.Description,
CreatedAt: product.CreatedAt.Format(time.RFC3339),
UpdatedAt: product.UpdatedAt.Format(time.RFC3339),
})
}
return res
}
+13 -11
View File
@@ -56,13 +56,15 @@ func (h *Handler) Create(c *gin.Context) {
return
}
req.PartnerID = ctx.GetPartnerID()
if err := req.Validate(); err != nil {
response.ErrorWrapper(c, errors.ErrorInvalidRequest)
return
}
res, err := h.service.Create(ctx, req.ToEntity())
ctx.IsSuperAdmin()
res, err := h.service.Create(ctx, req.ToEntity())
if err != nil {
response.ErrorWrapper(c, err)
return
@@ -260,16 +262,16 @@ func (h *Handler) Delete(c *gin.Context) {
func (h *Handler) toUserResponse(resp *entity.User) response.User {
return response.User{
ID: resp.ID,
Name: resp.Name,
Email: resp.Email,
Status: string(resp.Status),
RoleID: int64(resp.RoleID),
RoleName: resp.RoleName,
PartnerID: resp.PartnerID,
BranchName: resp.BranchName,
CreatedAt: resp.CreatedAt.Format(time.RFC3339),
UpdatedAt: resp.CreatedAt.Format(time.RFC3339),
ID: resp.ID,
Name: resp.Name,
Email: resp.Email,
Status: string(resp.Status),
RoleID: int64(resp.RoleID),
RoleName: resp.RoleName,
PartnerID: resp.PartnerID,
PartnerName: resp.PartnerName,
CreatedAt: resp.CreatedAt.Format(time.RFC3339),
UpdatedAt: resp.CreatedAt.Format(time.RFC3339),
}
}
+30
View File
@@ -26,6 +26,36 @@ type Partner struct {
Status string `json:"status"`
}
type CreatePartnerRequest struct {
Name string `json:"name" validate:"required"`
Address string `json:"address"`
Username string `json:"username" validate:"required"`
FullName string `json:"full_name"`
Email string `json:"email"`
Password string `json:"password" validate:"required"`
NIK string `json:"nik"`
PhoneNumber string `json:"phone_number"`
BankName string `json:"bank_name"`
BankAccountNumber string `json:"bank_account_number"`
BankAccountHolderName string `json:"bank_account_holder_name"`
}
func (e *CreatePartnerRequest) ToEntity() *entity.CreatePartnerRequest {
return &entity.CreatePartnerRequest{
Name: e.Name,
Address: e.Address,
Username: e.Username,
FullName: e.FullName,
Email: e.Email,
Password: e.Password,
NIK: e.NIK,
PhoneNumber: e.PhoneNumber,
BankName: e.BankName,
BankAccountNumber: e.BankAccountNumber,
BankAccountHolderName: e.BankAccountHolderName,
}
}
func (e *Partner) ToEntity() *entity.Partner {
return &entity.Partner{
Name: e.Name,
+9 -11
View File
@@ -28,14 +28,15 @@ func (p *ProductParam) ToEntity() entity.ProductSearch {
}
type Product struct {
Name string `json:"name" validate:"required"`
Type product.ProductType `json:"type" validate:"required"`
Price float64 `json:"price" validate:"required"`
Status product.ProductStatus `json:"status" validate:"required"`
Description string `json:"description" `
Image string `json:"image" `
BranchID int64 `json:"branch_id" validate:"required"`
StockQty int64 `json:"stock_qty" `
ID int64 `json:"id,omitempty"`
PartnerID int64 `json:"partner_id"`
Name string `json:"name" validate:"required"`
Type string `json:"type"`
Price float64 `json:"price" validate:"required"`
IsWeekendTicket bool `json:"is_weekend_ticket"`
IsSeasonTicket bool `json:"is_season_ticket"`
Status string `json:"status"`
Description string `json:"description" validate:"required"`
}
func (e *Product) ToEntity() *entity.Product {
@@ -45,8 +46,5 @@ func (e *Product) ToEntity() *entity.Product {
Price: e.Price,
Status: e.Status,
Description: e.Description,
Image: e.Image,
BranchID: e.BranchID,
StockQty: e.StockQty,
}
}
+75
View File
@@ -0,0 +1,75 @@
package request
import (
"furtuna-be/internal/entity"
)
type Site struct {
ID int64 `json:"id"`
Name string `json:"name" validate:"required"`
PartnerID *int64 `json:"partner_id"`
Image string `json:"image"`
Address string `json:"address"`
LocationLink string `json:"location_link"`
Description string `json:"description"`
Highlight string `json:"highlight"`
ContactPerson string `json:"contact_person"`
TnC string `json:"tnc"`
AdditionalInfo string `json:"additional_info"`
Status string `json:"status"`
IsSeasonTicket bool `json:"is_season_ticket"`
IsDiscountActive bool `json:"is_discount_active"`
Products []Product `json:"products"`
}
func (r *Site) ToEntity(createdBy int64) *entity.Site {
var products []entity.Product
for _, p := range r.Products {
products = append(products, entity.Product{
ID: p.ID,
PartnerID: *r.PartnerID,
Name: p.Name,
Type: p.Type,
Price: p.Price,
IsWeekendTicket: p.IsWeekendTicket,
IsSeasonTicket: p.IsSeasonTicket,
Status: p.Status,
Description: p.Description,
CreatedBy: createdBy,
})
}
return &entity.Site{
ID: r.ID,
Name: r.Name,
PartnerID: *r.PartnerID,
Image: r.Image,
Address: r.Address,
LocationLink: r.LocationLink,
Description: r.Description,
Highlight: r.Highlight,
ContactPerson: r.ContactPerson,
TnC: r.TnC,
AdditionalInfo: r.AdditionalInfo,
Status: r.Status,
IsSeasonTicket: r.IsSeasonTicket,
IsDiscountActive: r.IsDiscountActive,
Products: products,
}
}
type SiteParam struct {
Search string `form:"search"`
Name string `form:"name"`
Limit int `form:"limit,default=10"`
Offset int `form:"offset,default=0"`
}
func (r *SiteParam) ToEntity() entity.SiteSearch {
return entity.SiteSearch{
Search: r.Search,
Name: r.Name,
Limit: r.Limit,
Offset: r.Offset,
}
}
+4 -4
View File
@@ -1,10 +1,10 @@
package response
type LoginResponse struct {
Token string `json:"token"`
Name string `json:"name"`
Role Role `json:"role"`
Branch *Branch `json:"branch"`
Token string `json:"token"`
Name string `json:"name"`
Role Role `json:"role"`
Partner *Partner `json:"partner"`
}
type Role struct {
+3 -3
View File
@@ -4,9 +4,9 @@ type Partner struct {
ID *int64 `json:"id"`
Name string `json:"name"`
Status string `json:"status"`
Address string `json:"address"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
Address string `json:"address,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
}
type PartnerList struct {
+12 -13
View File
@@ -1,19 +1,18 @@
package response
import "furtuna-be/internal/constants/product"
type Product struct {
ID int64 `json:"id"`
Name string `json:"name"`
Type product.ProductType `json:"type"`
Price float64 `json:"price"`
Status product.ProductStatus `json:"status"`
Description string `json:"description" `
Image string `json:"image" `
BranchID int64 `json:"branch_id"`
StockQty int64 `json:"stock_qty"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
ID int64 `json:"id"`
PartnerID int64 `json:"partner_id"`
SiteID int64 `json:"site_id"`
Name string `json:"name"`
Type string `json:"type"`
Price float64 `json:"price"`
IsWeekendTicket bool `json:"is_weekend_ticket"`
IsSeasonTicket bool `json:"is_season_ticket"`
Status string `json:"status"`
Description string `json:"description"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
type ProductList struct {
+27
View File
@@ -0,0 +1,27 @@
package response
type Site struct {
ID *int64 `json:"id"`
Name string `json:"name"`
PartnerID int64 `json:"partner_id"`
Image string `json:"image"`
Address string `json:"address"`
LocationLink string `json:"location_link"`
Description string `json:"description"`
Highlight string `json:"highlight"`
ContactPerson string `json:"contact_person"`
TnC string `json:"tnc"`
AdditionalInfo string `json:"additional_info"`
Status string `json:"status"`
IsSeasonTicket bool `json:"is_season_ticket"`
IsDiscountActive bool `json:"is_discount_active"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
type SiteList struct {
Sites []Site `json:"sites"`
Total int64 `json:"total"`
Limit int `json:"limit"`
Offset int `json:"offset"`
}
+10 -10
View File
@@ -1,16 +1,16 @@
package response
type User struct {
ID int64 `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
Status string `json:"status"`
RoleID int64 `json:"role_id"`
RoleName string `json:"role_name"`
PartnerID *int64 `json:"partner_id"`
BranchName string `json:"partner_name"`
CreatedAt string `json:"created_at,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
ID int64 `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
Status string `json:"status"`
RoleID int64 `json:"role_id"`
RoleName string `json:"role_name"`
PartnerID *int64 `json:"partner_id"`
PartnerName string `json:"partner_name"`
CreatedAt string `json:"created_at,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
}
type UserList struct {