mirror of
https://github.com/sysadminsmedia/homebox.git
synced 2026-01-02 11:07:21 +01:00
Add entitytypes delete method
This commit is contained in:
@@ -79,3 +79,21 @@ func (ctrl *V1Controller) HandleEntityTypeUpdate() errchain.HandlerFunc {
|
||||
}
|
||||
return adapters.ActionID("id", fn, http.StatusOK)
|
||||
}
|
||||
|
||||
// HandleEntityTypeDelete godoc
|
||||
//
|
||||
// @Summary Delete Entity Type
|
||||
// @Tags EntityTypes
|
||||
// @Param id path string true "Entity Type ID"
|
||||
// @Param payload body repo.EntityTypeDelete true "Entity Type Delete Options"
|
||||
// @Success 204
|
||||
// @Router /v1/entitytype/{id} [DELETE]
|
||||
// @Security Bearer
|
||||
func (ctrl *V1Controller) HandleEntityTypeDelete() errchain.HandlerFunc {
|
||||
fn := func(r *http.Request, entityTypeID uuid.UUID, body repo.EntityTypeDelete) (any, error) {
|
||||
auth := services.NewContext(r.Context())
|
||||
err := ctrl.repo.EntityType.DeleteEntityType(auth, auth.GID, entityTypeID, body)
|
||||
return nil, err
|
||||
}
|
||||
return adapters.ActionID("id", fn, http.StatusNoContent)
|
||||
}
|
||||
|
||||
@@ -190,6 +190,7 @@ func (a *app) mountRoutes(r *chi.Mux, chain *errchain.ErrChain, repos *repo.AllR
|
||||
r.Post("/entitytype", chain.ToHandlerFunc(v1Ctrl.HandleEntityTypeCreate(), userMW...))
|
||||
r.Get("/entitytype/{id}", chain.ToHandlerFunc(v1Ctrl.HandleEntityTypeGetOne(), userMW...))
|
||||
r.Put("/entitytype/{id}", chain.ToHandlerFunc(v1Ctrl.HandleEntityTypeUpdate(), userMW...))
|
||||
r.Delete("/entitytype/{id}", chain.ToHandlerFunc(v1Ctrl.HandleEntityTypeDelete(), userMW...))
|
||||
|
||||
r.NotFound(http.NotFound)
|
||||
})
|
||||
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -746,6 +746,11 @@ definitions:
|
||||
- isLocation
|
||||
- name
|
||||
type: object
|
||||
repo.EntityTypeDelete:
|
||||
properties:
|
||||
replacementId:
|
||||
$ref: '#/definitions/uuid.NullUUID'
|
||||
type: object
|
||||
repo.EntityTypeUpdate:
|
||||
properties:
|
||||
color:
|
||||
@@ -1441,6 +1446,14 @@ definitions:
|
||||
- DefaultRole
|
||||
- RoleUser
|
||||
- RoleOwner
|
||||
uuid.NullUUID:
|
||||
properties:
|
||||
uuid:
|
||||
type: string
|
||||
valid:
|
||||
description: Valid is true if UUID is not NULL
|
||||
type: boolean
|
||||
type: object
|
||||
v1.APISummary:
|
||||
properties:
|
||||
allowRegistration:
|
||||
@@ -1871,6 +1884,27 @@ paths:
|
||||
tags:
|
||||
- EntityTypes
|
||||
/v1/entitytype/{id}:
|
||||
delete:
|
||||
parameters:
|
||||
- description: Entity Type ID
|
||||
in: path
|
||||
name: id
|
||||
required: true
|
||||
type: string
|
||||
- description: Entity Type Delete Options
|
||||
in: body
|
||||
name: payload
|
||||
required: true
|
||||
schema:
|
||||
$ref: '#/definitions/repo.EntityTypeDelete'
|
||||
responses:
|
||||
"204":
|
||||
description: No Content
|
||||
security:
|
||||
- Bearer: []
|
||||
summary: Delete Entity Type
|
||||
tags:
|
||||
- EntityTypes
|
||||
get:
|
||||
parameters:
|
||||
- description: Entity Type ID
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"github.com/google/uuid"
|
||||
"github.com/sysadminsmedia/homebox/backend/internal/core/services/reporting/eventbus"
|
||||
"github.com/sysadminsmedia/homebox/backend/internal/data/ent"
|
||||
"github.com/sysadminsmedia/homebox/backend/internal/data/ent/entity"
|
||||
"github.com/sysadminsmedia/homebox/backend/internal/data/ent/entitytype"
|
||||
"github.com/sysadminsmedia/homebox/backend/internal/data/ent/group"
|
||||
)
|
||||
@@ -38,6 +39,10 @@ type (
|
||||
Icon string `json:"icon" extension:"x-nullable"`
|
||||
Color string `json:"color" extension:"x-nullable"`
|
||||
}
|
||||
|
||||
EntityTypeDelete struct {
|
||||
ReplacementId uuid.NullUUID `json:"replacementId" extension:"x-nullable"`
|
||||
}
|
||||
)
|
||||
|
||||
func (e *EntityTypeRepository) CreateDefaultEntityTypes(ctx context.Context, gid uuid.UUID) error {
|
||||
@@ -133,3 +138,17 @@ func (e *EntityTypeRepository) UpdateEntityType(ctx context.Context, gid uuid.UU
|
||||
}
|
||||
return et, nil
|
||||
}
|
||||
|
||||
func (e *EntityTypeRepository) DeleteEntityType(ctx context.Context, gid uuid.UUID, id uuid.UUID, data EntityTypeDelete) error {
|
||||
del := e.db.EntityType.Delete().Where(entitytype.HasGroupWith(group.ID(gid)), entitytype.ID(id))
|
||||
if data.ReplacementId.Valid {
|
||||
del = del.Where(entitytype.IDNEQ(data.ReplacementId.UUID))
|
||||
// Reassign entities to replacement type
|
||||
_, err := e.db.Entity.Update().Where(entity.HasTypeWith(entitytype.ID(id))).SetTypeID(data.ReplacementId.UUID).Save(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
_, err := del.Exec(ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -746,6 +746,11 @@ definitions:
|
||||
- isLocation
|
||||
- name
|
||||
type: object
|
||||
repo.EntityTypeDelete:
|
||||
properties:
|
||||
replacementId:
|
||||
$ref: '#/definitions/uuid.NullUUID'
|
||||
type: object
|
||||
repo.EntityTypeUpdate:
|
||||
properties:
|
||||
color:
|
||||
@@ -1441,6 +1446,14 @@ definitions:
|
||||
- DefaultRole
|
||||
- RoleUser
|
||||
- RoleOwner
|
||||
uuid.NullUUID:
|
||||
properties:
|
||||
uuid:
|
||||
type: string
|
||||
valid:
|
||||
description: Valid is true if UUID is not NULL
|
||||
type: boolean
|
||||
type: object
|
||||
v1.APISummary:
|
||||
properties:
|
||||
allowRegistration:
|
||||
@@ -1871,6 +1884,27 @@ paths:
|
||||
tags:
|
||||
- EntityTypes
|
||||
/v1/entitytype/{id}:
|
||||
delete:
|
||||
parameters:
|
||||
- description: Entity Type ID
|
||||
in: path
|
||||
name: id
|
||||
required: true
|
||||
type: string
|
||||
- description: Entity Type Delete Options
|
||||
in: body
|
||||
name: payload
|
||||
required: true
|
||||
schema:
|
||||
$ref: '#/definitions/repo.EntityTypeDelete'
|
||||
responses:
|
||||
"204":
|
||||
description: No Content
|
||||
security:
|
||||
- Bearer: []
|
||||
summary: Delete Entity Type
|
||||
tags:
|
||||
- EntityTypes
|
||||
get:
|
||||
parameters:
|
||||
- description: Entity Type ID
|
||||
|
||||
@@ -511,6 +511,10 @@ export interface EntityTypeCreate {
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface EntityTypeDelete {
|
||||
replacementId: UuidNullUUID;
|
||||
}
|
||||
|
||||
export interface EntityTypeUpdate {
|
||||
color: string;
|
||||
description: string;
|
||||
@@ -890,6 +894,12 @@ export interface UserRegistration {
|
||||
token: string;
|
||||
}
|
||||
|
||||
export interface UuidNullUUID {
|
||||
uuid: string;
|
||||
/** Valid is true if UUID is not NULL */
|
||||
valid: boolean;
|
||||
}
|
||||
|
||||
export interface APISummary {
|
||||
allowRegistration: boolean;
|
||||
build: Build;
|
||||
|
||||
Reference in New Issue
Block a user