diff --git a/api/accomodation/gate.controllers.go b/api/accomodation/gate.controllers.go index a60f38f0..e561a487 100644 --- a/api/accomodation/gate.controllers.go +++ b/api/accomodation/gate.controllers.go @@ -218,6 +218,47 @@ func UpdateAccommodationById(c *gin.Context) { pkg.Log.SuccessCtx(c) } +func DeleteAccommodationById(c *gin.Context) { + accIdStr := c.Param("accId") + accId, ok := pkg.GrabUuid(c, accIdStr, "DELETE-ACCOMMODATION", "Accommodation") + if !ok { + return + } + + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + conn, err := cmd.DBPool.Acquire(ctx) + if pkg.HandleDbAcquireErr(c, err, "DELETE-ACCOMMODATION") { + return + } + defer conn.Release() + + q := db.New() + + row, err := q.DeleteAccommodationByIdQuery(ctx, conn, accId) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{ + "message": "Oops! Something happened. Please try again later.", + }) + pkg.Log.ErrorCtx(c, "[DELETE-ACCOMMODATION-ERROR]: Failed to delete accommodation request", err) + return + } + if row == 0 { + c.JSON(http.StatusNotFound, gin.H{ + "message": "Accommodation request not found", + }) + pkg.Log.WarnCtx(c, "[DELETE-ACCOMMODATION-WARN]: Accommodation ID does not exist") + return + } + + c.JSON(http.StatusOK, gin.H{ + "message": "Accommodation request deleted successfully", + }) + pkg.Log.SuccessCtx(c) + +} + func GateCheckIn(c *gin.Context) { personellIdStr, ok := pkg.GrabUserId(c, "GATE") if !ok { diff --git a/api/accomodation/routes.go b/api/accomodation/routes.go index 56e9d6d7..a5b0d7ad 100644 --- a/api/accomodation/routes.go +++ b/api/accomodation/routes.go @@ -44,6 +44,7 @@ func GateRoutes(r *gin.RouterGroup) { r.POST("/app/map", mw.Auth, mw.CheckHospitality, mw.CheckGate, MapQrStudentId) r.GET("/app/:accId", mw.Auth, mw.CheckHospitality, mw.CheckGate, GetAccommodationById) r.PUT("/app/:accId", mw.Auth, mw.CheckHospitality, mw.CheckGate, UpdateAccommodationById) + r.DELETE("/app/:accId", mw.Auth, mw.CheckHospitality, mw.CheckGate, DeleteAccommodationById) r.GET("/app/gate/status/check-in/:hospId", mw.Auth, mw.CheckHospitality, mw.CheckGate, GateCheckInStatus) r.POST("/app/gate/check-in/:hospId", mw.Auth, mw.CheckHospitality, mw.CheckGate, GateCheckIn) diff --git a/bruno/hospitality (all)/DeleteAccommodation.bru b/bruno/hospitality (all)/DeleteAccommodation.bru new file mode 100644 index 00000000..53c772ef --- /dev/null +++ b/bruno/hospitality (all)/DeleteAccommodation.bru @@ -0,0 +1,20 @@ +meta { + name: DeleteAccommodation + type: http + seq: 22 +} + +delete { + url: {{baseUrl}}/accommodation/app/:accId + body: none + auth: inherit +} + +params:path { + accId: 008c88d4-332f-4246-845c-f8d4c234c557 +} + +settings { + encodeUrl: true + timeout: 0 +} diff --git a/db/gen/accomodation-for-hospitality-panel.sql.go b/db/gen/accomodation-for-hospitality-panel.sql.go index 1ee56459..6a31cf01 100644 --- a/db/gen/accomodation-for-hospitality-panel.sql.go +++ b/db/gen/accomodation-for-hospitality-panel.sql.go @@ -172,6 +172,19 @@ func (q *Queries) AllotHostelQuery(ctx context.Context, db DBTX, arg AllotHostel return result.RowsAffected(), nil } +const deleteAccommodationByIdQuery = `-- name: DeleteAccommodationByIdQuery :execrows +DELETE FROM accomodation_details +WHERE id = $1 +` + +func (q *Queries) DeleteAccommodationByIdQuery(ctx context.Context, db DBTX, id uuid.UUID) (int64, error) { + result, err := db.Exec(ctx, deleteAccommodationByIdQuery, id) + if err != nil { + return 0, err + } + return result.RowsAffected(), nil +} + const deleteHostelQuery = `-- name: DeleteHostelQuery :execrows DELETE FROM hostel_metadata WHERE id = $1 diff --git a/db/queries/accomodation-for-hospitality-panel.sql b/db/queries/accomodation-for-hospitality-panel.sql index 62398c85..12a82c78 100644 --- a/db/queries/accomodation-for-hospitality-panel.sql +++ b/db/queries/accomodation-for-hospitality-panel.sql @@ -160,6 +160,10 @@ SET updated_at = NOW() WHERE id = $1; +-- name: DeleteAccommodationByIdQuery :execrows +DELETE FROM accomodation_details +WHERE id = $1; + -- name: MapQrStudentIdQuery :one UPDATE student SET hospitality_id = $2