Skip to content

Commit

Permalink
Feat: Add get all categories API
Browse files Browse the repository at this point in the history
  • Loading branch information
irfan-arrosid committed Mar 28, 2024
1 parent acc2f03 commit 6cf96b2
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
PORT=":8080"
DSN_MYSQL="USER:PASSWORD@tcp(HOST:PORT)/DATABASE"
DSN_MYSQL="USER:PASSWORD@tcp(HOST:PORT)/DATABASE?parseTime=true"
1 change: 1 addition & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func main() {

// PRODUCT endpoints
api.POST("/categories", productHandler.NewCategory)
api.GET("/categories", productHandler.GetCategories)

r.Run(os.Getenv("PORT"))
}
35 changes: 24 additions & 11 deletions internal/app/product/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,44 @@ package product

import (
"goshop-api/internal/app/user"
"time"

"github.com/google/uuid"
"gorm.io/gorm"
)

type Product struct {
Id int
UserId int
Id string `gorm:"type:varchar(36);primaryKey"`
UserId string
Name string
Description string
CategoryId int
CategoryId string
Price float64
Quantity int
ImageURL string
User user.User // Product can only belong to single User
Category Category // Product can only belong to single Category
gorm.Model
CreatedAt time.Time
UpdatedAt time.Time
}

type Category struct {
Id int
Name string
gorm.Model
Id string `gorm:"type:varchar(36);primaryKey"`
Name string
CreatedAt time.Time
UpdatedAt time.Time
}

// func (product *Product) BeforeCreate(tx *gorm.DB) (err error) {
// product.Id = uuid.New()
// return nil
// }
func (product *Product) BeforeCreate(tx *gorm.DB) (err error) {
if product.Id == "" {
product.Id = uuid.New().String() // should be string in mysql
}
return nil
}

func (category *Category) BeforeCreate(tx *gorm.DB) (err error) {
if category.Id == "" {
category.Id = uuid.New().String() // should be string in mysql
}
return nil
}
13 changes: 13 additions & 0 deletions internal/app/product/formatter.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
package product

type CategoryFormatter struct {
Id string `json:"id"`
Name string `json:"name"`
}

func FormatCategory(category Category) CategoryFormatter {
categoryFormatter := CategoryFormatter{}
categoryFormatter.Id = category.Id
categoryFormatter.Name = category.Name

return categoryFormatter
}

func FormatCategories(categories []Category) []CategoryFormatter {
categoriesFormatter := []CategoryFormatter{}

for _, category := range categories {
categoryFormatter := FormatCategory((category))
categoriesFormatter = append(categoriesFormatter, categoryFormatter)
}

return categoriesFormatter
}
11 changes: 11 additions & 0 deletions internal/app/product/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "gorm.io/gorm"

type Repository interface {
CreateCategory(category Category) (Category, error)
FindAllCategory() ([]Category, error)
}

type repository struct {
Expand All @@ -22,3 +23,13 @@ func (r *repository) CreateCategory(category Category) (Category, error) {

return category, nil
}

func (r *repository) FindAllCategory() ([]Category, error) {
var categories []Category
err := r.db.Find(&categories).Error
if err != nil {
return categories, err
}

return categories, nil
}
10 changes: 10 additions & 0 deletions internal/app/product/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package product

type Service interface {
NewCategory(input CreateCategoryInput) (Category, error)
GetCategories() ([]Category, error)
}

type service struct {
Expand All @@ -23,3 +24,12 @@ func (s *service) NewCategory(input CreateCategoryInput) (Category, error) {

return newCategory, nil
}

func (s *service) GetCategories() ([]Category, error) {
categories, err := s.repository.FindAllCategory()
if err != nil {
return categories, err
}

return categories, nil
}
28 changes: 17 additions & 11 deletions internal/app/user/entity.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
package user

import (
"time"

"github.com/google/uuid"
"gorm.io/gorm"
)

type User struct {
Id int
Name string
Email string
Password string
Address string
IsAdmin bool
gorm.Model
Id string `gorm:"type:varchar(36);primaryKey"`
Name string
Email string
Password string
Address string
IsAdmin bool
CreatedAt time.Time
UpdatedAt time.Time
}

// func (user *User) BeforeCreate(tx *gorm.DB) (err error) {
// user.Id = uuid.New()
// return nil
// }
func (user *User) BeforeCreate(tx *gorm.DB) (err error) {
if user.Id == "" {
user.Id = uuid.New().String() // should be string in mysql
}
return nil
}
15 changes: 15 additions & 0 deletions internal/handler/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,18 @@ func (h *productHandler) NewCategory(c *gin.Context) {

c.JSON(http.StatusOK, response)
}

func (h *productHandler) GetCategories(c *gin.Context) {
categories, err := h.productService.GetCategories()
if err != nil {
response := helper.APIResponse("Error to get categories", http.StatusBadRequest, "error", nil)

c.JSON(http.StatusBadRequest, response)
return
}

formatter := product.FormatCategories(categories)
response := helper.APIResponse("Successfully get categories", http.StatusOK, "success", formatter)

c.JSON(http.StatusOK, response)
}

0 comments on commit 6cf96b2

Please sign in to comment.