mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
[FIX/BE-US] changes role to user and query
This commit is contained in:
@@ -103,6 +103,9 @@ func (s *kandangService) CreateOne(c *fiber.Ctx, req *validation.Create) (*entit
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := m.EnsureLocationAccess(c, s.Repository.DB(), req.LocationId); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if exists, err := s.Repository.NameExists(c.Context(), req.Name, nil); err != nil {
|
||||
s.Log.Errorf("Failed to check kandang name: %+v", err)
|
||||
@@ -177,6 +180,14 @@ func (s kandangService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := m.EnsureKandangAccess(c, s.Repository.DB(), id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if req.LocationId != nil {
|
||||
if err := m.EnsureLocationAccess(c, s.Repository.DB(), *req.LocationId); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
existing, err := s.Repository.GetByID(c.Context(), id, nil)
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -268,6 +279,10 @@ func (s kandangService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint)
|
||||
}
|
||||
|
||||
func (s kandangService) DeleteOne(c *fiber.Ctx, id uint) error {
|
||||
if err := m.EnsureKandangAccess(c, s.Repository.DB(), id); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := s.Repository.DeleteOne(c.Context(), id); err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusNotFound, "Kandang not found")
|
||||
|
||||
@@ -128,6 +128,19 @@ func (s *warehouseService) CreateOne(c *fiber.Ctx, req *validation.Create) (*ent
|
||||
if err := validateWarehouseTypeRequirements(typ, &req.AreaId, req.LocationId, req.KandangId); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := m.EnsureAreaAccess(c, s.Repository.DB(), req.AreaId); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if req.LocationId != nil {
|
||||
if err := m.EnsureLocationAccess(c, s.Repository.DB(), *req.LocationId); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if req.KandangId != nil {
|
||||
if err := m.EnsureKandangAccess(c, s.Repository.DB(), *req.KandangId); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
//? Check relation area, location, and kandang
|
||||
if err := common.EnsureRelations(c.Context(),
|
||||
@@ -166,6 +179,21 @@ func (s warehouseService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uin
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if req.AreaId != nil {
|
||||
if err := m.EnsureAreaAccess(c, s.Repository.DB(), *req.AreaId); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if req.LocationId != nil {
|
||||
if err := m.EnsureLocationAccess(c, s.Repository.DB(), *req.LocationId); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if req.KandangId != nil {
|
||||
if err := m.EnsureKandangAccess(c, s.Repository.DB(), *req.KandangId); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
existing, err := s.GetOne(c, id)
|
||||
if err != nil {
|
||||
@@ -243,6 +271,10 @@ func (s warehouseService) UpdateOne(c *fiber.Ctx, req *validation.Update, id uin
|
||||
}
|
||||
|
||||
func (s warehouseService) DeleteOne(c *fiber.Ctx, id uint) error {
|
||||
if err := m.EnsureWarehouseAccess(c, s.Repository.DB(), id); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := s.Repository.DeleteOne(c.Context(), id); err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusNotFound, "Warehouse not found")
|
||||
|
||||
Reference in New Issue
Block a user