fix(menu): Update ParentID handling in MenuUpdateRequest and improve Update method for better clarity
Deploy Application / deploy (push) Successful in 8s
Details
Deploy Application / deploy (push) Successful in 8s
Details
This commit is contained in:
parent
fa9c1ba44f
commit
170d284d08
|
|
@ -92,6 +92,6 @@ type (
|
|||
Status string `json:"status" form:"status" binding:"omitempty"`
|
||||
Mode string `json:"mode" form:"mode" binding:"omitempty,min=1,max=20"`
|
||||
Sequence int `json:"sequence" form:"sequence" binding:"omitempty"`
|
||||
ParentID *uuid.UUID `json:"parent_id" form:"parent_id" binding:"omitempty,uuid4"`
|
||||
ParentID *uuid.UUID `json:"parent_id" form:"parent_id" binding:"omitempty"`
|
||||
}
|
||||
)
|
||||
|
|
|
|||
|
|
@ -86,7 +86,10 @@ func (m *menuRepository) Update(ctx context.Context, tx *gorm.DB, menu entities.
|
|||
tx = m.db
|
||||
}
|
||||
|
||||
if err := tx.WithContext(ctx).Updates(&menu).Error; err != nil {
|
||||
if err := tx.WithContext(ctx).Model(&entities.M_Menu{}).
|
||||
Where("id = ?", menu.ID).
|
||||
Select("*").
|
||||
Updates(&menu).Error; err != nil {
|
||||
return entities.M_Menu{}, err
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -10,7 +10,6 @@ import (
|
|||
"github.com/Caknoooo/go-gin-clean-starter/modules/menu/repository"
|
||||
staticDto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto"
|
||||
"github.com/google/uuid"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
|
|
@ -167,15 +166,12 @@ func (m *menuService) GetByName(ctx context.Context, name string) (dto.MenuRespo
|
|||
|
||||
// Update implements MenuService.
|
||||
func (m *menuService) Update(ctx context.Context, menuId string, menu dto.MenuUpdateRequest) (dto.MenuResponse, error) {
|
||||
// ensure exists
|
||||
existing, err := m.menuRepository.GetById(ctx, m.db, menuId)
|
||||
if err != nil {
|
||||
return dto.MenuResponse{}, dto.ErrMenuNotFound
|
||||
}
|
||||
|
||||
// Only update fields if provided (not empty)
|
||||
if menu.Name != "" {
|
||||
// Check for duplicate name
|
||||
menuByName, err := m.menuRepository.GetByName(ctx, m.db, menu.Name)
|
||||
if err == nil && menuByName.ID != existing.ID {
|
||||
return dto.MenuResponse{}, errors.New("menu name already exists")
|
||||
|
|
@ -197,13 +193,10 @@ func (m *menuService) Update(ctx context.Context, menuId string, menu dto.MenuUp
|
|||
if menu.Sequence != 0 {
|
||||
existing.Sequence = menu.Sequence
|
||||
}
|
||||
if menu.ParentID != nil && menu.ParentID.String() != "" {
|
||||
pid, err := uuid.Parse(menu.ParentID.String())
|
||||
if err != nil {
|
||||
return dto.MenuResponse{}, err
|
||||
}
|
||||
if menu.ParentID != nil {
|
||||
pid := *menu.ParentID
|
||||
existing.ParentID = &pid
|
||||
} else if menu.ParentID != nil && menu.ParentID.String() == "" {
|
||||
} else {
|
||||
existing.ParentID = nil
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue