@@ -6,78 +6,61 @@ import (
66 "strings"
77
88 "github.com/gin-gonic/gin"
9+ "github.com/jackc/pgx/v5"
10+ "github.com/sndcds/grains/grains_api"
11+ "github.com/sndcds/uranus/app"
912)
1013
11- // TODO: Review code
12-
1314func (h * ApiHandler ) AdminCreateSpace (gc * gin.Context ) {
1415 ctx := gc .Request .Context ()
16+ userId := h .userId (gc )
17+ apiRequest := grains_api .NewRequest (gc , "admin-create-space" )
1518
16- type UpdateRequest struct {
17- VenueId int `json:"venue_id"`
18- Name * string `json:"name"`
19- Description * string `json:"description"`
20- SpaceTypeId int `json:"space_type_id"`
21- BuildingLevel int `json:"building_level"`
22- TotalCapacity int `json:"total_capacity"`
23- SeatingCapacity int `json:"seating_capacity"`
24- WebsiteLink * string `json:"website_link"`
25- AccessibilityFlags int64 `json:"accessibility_flags"`
26- AccessibilitySummary * string `json:"accessibility_summary"`
19+ type Payload struct {
20+ OrganizationId int `json:"organization_id" binding:"required"`
21+ VenueId int `json:"venue_id" binding:"required"`
22+ SpaceName string `json:"space_name" binding:"required"`
2723 }
28-
29- // TODO: Check permissions by user and OrganizationId
30-
31- var req UpdateRequest
32- if err := gc .ShouldBindJSON (& req ); err != nil {
33- gc .JSON (http .StatusBadRequest , gin.H {"error" : err .Error ()})
24+ payload , ok := grains_api .DecodeJSONBody [Payload ](gc , apiRequest )
25+ if ! ok {
3426 return
3527 }
3628
37- // Begin transaction
38- tx , err := h .DbPool .Begin (gc )
39- if err != nil {
40- gc .JSON (http .StatusInternalServerError , gin.H {"error" : "failed to start transaction" })
29+ spaceName := strings .TrimSpace (payload .SpaceName )
30+ if spaceName == "" {
31+ apiRequest .Error (http .StatusBadRequest , "space_name cannot be empty" )
4132 return
4233 }
43- defer func () { _ = tx .Rollback (ctx ) }()
4434
45- var newId int
46- insertSpaceQuery := `
47- INSERT INTO {{schema}}.space
48- (venue_id, name, description, space_type_id, building_level, total_capacity, seating_capacity, website_link, accessibility_flags, accessibility_summary)
49- VALUES
50- ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
51- RETURNING id
52- `
53- insertSpaceQuery = strings .Replace (insertSpaceQuery , "{{schema}}" , h .DbSchema , 1 )
35+ apiRequest .Metadata ["prganization_id" ] = payload .OrganizationId
36+ apiRequest .Metadata ["venue_id" ] = payload .VenueId
37+ apiRequest .Metadata ["space_name" ] = spaceName
5438
55- err = tx .QueryRow (gc , insertSpaceQuery ,
56- req .VenueId ,
57- req .Name ,
58- req .Description ,
59- req .SpaceTypeId ,
60- req .BuildingLevel ,
61- req .TotalCapacity ,
62- req .SeatingCapacity ,
63- req .WebsiteLink ,
64- req .AccessibilityFlags ,
65- req .AccessibilitySummary ,
66- ).Scan (& newId )
39+ txErr := WithTransaction (ctx , h .DbPool , func (tx pgx.Tx ) * ApiTxError {
6740
68- if err != nil {
69- gc .JSON (http .StatusBadRequest , gin.H {"error" : fmt .Sprintf ("insert space failed: %v" , err )})
70- return
71- }
41+ txErr := h .CheckOrganizationAllPermissions (
42+ gc , tx , userId , payload .OrganizationId ,
43+ app .PermAddSpace )
44+ if txErr != nil {
45+ return txErr
46+ }
7247
73- // Commit transaction
74- if err = tx .Commit (gc ); err != nil {
75- gc .JSON (http .StatusInternalServerError , gin.H {"error" : "failed to commit transaction" })
48+ newSpaceId := - 1
49+ query := fmt .Sprintf (`INSERT INTO %s.space (venue_id, name) VALUES ($1, $2) RETURNING id` , h .DbSchema )
50+ err := tx .QueryRow (ctx , query , payload .VenueId , spaceName ).Scan (& newSpaceId )
51+ if err != nil {
52+ return & ApiTxError {
53+ Code : http .StatusInternalServerError ,
54+ Err : fmt .Errorf ("Internal server error" ),
55+ }
56+ }
57+ apiRequest .Metadata ["space_id" ] = newSpaceId
58+ return nil
59+ })
60+ if txErr != nil {
61+ apiRequest .Error (txErr .Code , txErr .Error ())
7662 return
7763 }
7864
79- gc .JSON (http .StatusOK , gin.H {
80- "id" : newId ,
81- "message" : "Space created successfully" ,
82- })
65+ apiRequest .SuccessNoData (http .StatusOK , "space successfully created" )
8366}
0 commit comments