feat: add feature get data API for request pickup by user masy
This commit is contained in:
parent
32e789ce9f
commit
0e8adf0e81
|
@ -65,6 +65,8 @@ func InitDatabase() {
|
||||||
&domain.CoverageArea{},
|
&domain.CoverageArea{},
|
||||||
&domain.CoverageDistric{},
|
&domain.CoverageDistric{},
|
||||||
&domain.CoverageSubdistrict{},
|
&domain.CoverageSubdistrict{},
|
||||||
|
&domain.RequestPickup{},
|
||||||
|
&domain.RequestItem{},
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Error: Failed to auto migrate domain:", err)
|
log.Fatal("Error: Failed to auto migrate domain:", err)
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
package domain
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
type RequestPickup struct {
|
||||||
|
ID string `gorm:"primaryKey;type:uuid;default:uuid_generate_v4()" json:"id"`
|
||||||
|
UserID string `gorm:"not null" json:"userId"`
|
||||||
|
User User `gorm:"foreignKey:UserID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"user"`
|
||||||
|
Request []RequestItem `gorm:"foreignKey:RequestPickupID" json:"request"`
|
||||||
|
RequestTime string `json:"requestTime"`
|
||||||
|
UserAddressID string `json:"userAddressId"`
|
||||||
|
UserAddress Address `gorm:"foreignKey:UserAddressID" json:"userAddress"`
|
||||||
|
StatusRequest string `json:"statusRequest"`
|
||||||
|
CreatedAt time.Time `gorm:"default:current_timestamp" json:"createdAt"`
|
||||||
|
UpdatedAt time.Time `gorm:"default:current_timestamp" json:"updatedAt"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type RequestItem struct {
|
||||||
|
ID string `gorm:"primaryKey;type:uuid;default:uuid_generate_v4()" json:"id"`
|
||||||
|
TrashCategoryID string `gorm:"type:uuid;not null" json:"trashCategoryId"`
|
||||||
|
TrashCategory TrashCategory `gorm:"foreignKey:TrashCategoryID" json:"trashCategory"`
|
||||||
|
EstimatedAmount string `gorm:"not null" json:"estimatedAmount"`
|
||||||
|
RequestPickupID string `gorm:"type:uuid;not null" json:"requestPickupId"`
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,36 @@
|
||||||
|
package dto
|
||||||
|
|
||||||
|
type RequestPickupResponse struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
UserID string `json:"userId"`
|
||||||
|
Request []RequestItemDTO `json:"request"`
|
||||||
|
RequestTime string `json:"requestTimePickup"`
|
||||||
|
UserAddress UserAddressDTO `json:"userAddress"`
|
||||||
|
StatusRequest string `json:"status"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type RequestItemDTO struct {
|
||||||
|
TrashCategory string `json:"trash_category"`
|
||||||
|
EstimatedAmount string `json:"estimated_quantity"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type UserAddressDTO struct {
|
||||||
|
Province string `json:"province"`
|
||||||
|
District string `json:"district"`
|
||||||
|
Subdistrict string `json:"subdistrict"`
|
||||||
|
PostalCode int `json:"postalCode"`
|
||||||
|
Village string `json:"village"`
|
||||||
|
Detail string `json:"detail"`
|
||||||
|
Geography string `json:"geography"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRequestPickupResponse(id, userID, requestTime, statusRequest string, request []RequestItemDTO, userAddress UserAddressDTO) RequestPickupResponse {
|
||||||
|
return RequestPickupResponse{
|
||||||
|
ID: id,
|
||||||
|
UserID: userID,
|
||||||
|
Request: request,
|
||||||
|
RequestTime: requestTime,
|
||||||
|
UserAddress: userAddress,
|
||||||
|
StatusRequest: statusRequest,
|
||||||
|
}
|
||||||
|
}
|
|
@ -94,4 +94,8 @@ func AppRouter(app *fiber.App) {
|
||||||
api.Get("/wilayah-indonesia/regencies/:id", controllers.GetRegencyByID)
|
api.Get("/wilayah-indonesia/regencies/:id", controllers.GetRegencyByID)
|
||||||
api.Get("/wilayah-indonesia/subdistricts/:id", controllers.GetDistrictByID)
|
api.Get("/wilayah-indonesia/subdistricts/:id", controllers.GetDistrictByID)
|
||||||
api.Get("/wilayah-indonesia/villages/:id", controllers.GetVillageByID)
|
api.Get("/wilayah-indonesia/villages/:id", controllers.GetVillageByID)
|
||||||
|
|
||||||
|
// # request pickup by user (masyarakat) #
|
||||||
|
api.Get("/requestpickup", middleware.AuthMiddleware, controllers.GetRequestPickupsByUser)
|
||||||
|
// api.Post("/addrequestpickup", middleware.AuthMiddleware, controllers.CreateRequestPickup)
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,67 @@
|
||||||
|
package controllers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"github.com/pahmiudahgede/senggoldong/dto"
|
||||||
|
"github.com/pahmiudahgede/senggoldong/internal/services"
|
||||||
|
"github.com/pahmiudahgede/senggoldong/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetRequestPickupsByUser(c *fiber.Ctx) error {
|
||||||
|
|
||||||
|
userID := c.Locals("userID").(string)
|
||||||
|
if userID == "" {
|
||||||
|
return c.Status(fiber.StatusUnauthorized).JSON(utils.FormatResponse(
|
||||||
|
fiber.StatusUnauthorized,
|
||||||
|
"User not authenticated",
|
||||||
|
nil,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
requestPickups, err := services.GetRequestPickupsByUser(userID)
|
||||||
|
if err != nil {
|
||||||
|
return c.Status(fiber.StatusInternalServerError).JSON(utils.FormatResponse(
|
||||||
|
fiber.StatusInternalServerError,
|
||||||
|
"Failed to fetch request pickups",
|
||||||
|
nil,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
var requestPickupResponses []dto.RequestPickupResponse
|
||||||
|
for _, requestPickup := range requestPickups {
|
||||||
|
var requestItems []dto.RequestItemDTO
|
||||||
|
for _, item := range requestPickup.Request {
|
||||||
|
requestItems = append(requestItems, dto.RequestItemDTO{
|
||||||
|
TrashCategory: item.TrashCategory.Name,
|
||||||
|
EstimatedAmount: item.EstimatedAmount,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
userAddress := dto.UserAddressDTO{
|
||||||
|
Province: requestPickup.UserAddress.Province,
|
||||||
|
District: requestPickup.UserAddress.District,
|
||||||
|
Subdistrict: requestPickup.UserAddress.Subdistrict,
|
||||||
|
PostalCode: requestPickup.UserAddress.PostalCode,
|
||||||
|
Village: requestPickup.UserAddress.Village,
|
||||||
|
Detail: requestPickup.UserAddress.Detail,
|
||||||
|
Geography: requestPickup.UserAddress.Geography,
|
||||||
|
}
|
||||||
|
|
||||||
|
requestPickupResponse := dto.NewRequestPickupResponse(
|
||||||
|
requestPickup.ID,
|
||||||
|
requestPickup.UserID,
|
||||||
|
requestPickup.RequestTime,
|
||||||
|
requestPickup.StatusRequest,
|
||||||
|
requestItems,
|
||||||
|
userAddress,
|
||||||
|
)
|
||||||
|
|
||||||
|
requestPickupResponses = append(requestPickupResponses, requestPickupResponse)
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Status(fiber.StatusOK).JSON(utils.FormatResponse(
|
||||||
|
fiber.StatusOK,
|
||||||
|
"Request pickup by user has been fetched",
|
||||||
|
requestPickupResponses,
|
||||||
|
))
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
package repositories
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/pahmiudahgede/senggoldong/config"
|
||||||
|
"github.com/pahmiudahgede/senggoldong/domain"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetRequestPickupsByUser(userID string) ([]domain.RequestPickup, error) {
|
||||||
|
var requestPickups []domain.RequestPickup
|
||||||
|
|
||||||
|
err := config.DB.Preload("Request").
|
||||||
|
Preload("Request.TrashCategory").
|
||||||
|
Preload("UserAddress").
|
||||||
|
Where("user_id = ?", userID).
|
||||||
|
Find(&requestPickups).Error
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return requestPickups, nil
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
package services
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/pahmiudahgede/senggoldong/domain"
|
||||||
|
"github.com/pahmiudahgede/senggoldong/internal/repositories"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetRequestPickupsByUser(userID string) ([]domain.RequestPickup, error) {
|
||||||
|
|
||||||
|
return repositories.GetRequestPickupsByUser(userID)
|
||||||
|
}
|
Loading…
Reference in New Issue