Skip to content

Commit f48158a

Browse files
general refactoring
1 parent bede66c commit f48158a

File tree

83 files changed

+1640
-1073
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+1640
-1073
lines changed

api/admin_create_event.go

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ import (
1212

1313
"github.com/gin-gonic/gin"
1414
"github.com/jackc/pgx/v5"
15-
"github.com/sndcds/grains/grainsapi"
15+
"github.com/sndcds/grains/grains_api"
1616
"github.com/sndcds/uranus/app"
1717
"github.com/sndcds/uranus/model"
1818
)
1919

20-
type incomingEvent struct {
20+
type eventPayload struct {
2121
ContentLanguage string `json:"content_language" binding:"required"`
2222
ReleaseStatus *string `json:"release_status" binding:"required"`
2323
ReleaseDate *string `json:"release_date"`
@@ -54,7 +54,7 @@ type incomingEvent struct {
5454
func (h *ApiHandler) AdminCreateEvent(gc *gin.Context) {
5555
ctx := gc.Request.Context()
5656
userId := h.userId(gc)
57-
apiRequest := grainsapi.NewRequest(gc, "create-event")
57+
apiRequest := grains_api.NewRequest(gc, "create-event")
5858

5959
// Read the body
6060
body, err := io.ReadAll(gc.Request.Body)
@@ -70,7 +70,7 @@ func (h *ApiHandler) AdminCreateEvent(gc *gin.Context) {
7070
decoder := json.NewDecoder(bytes.NewReader(body))
7171
decoder.DisallowUnknownFields()
7272

73-
var payload incomingEvent
73+
var payload eventPayload
7474
err = decoder.Decode(&payload)
7575
if err != nil {
7676
var syntaxErr *json.SyntaxError
@@ -151,13 +151,13 @@ func (h *ApiHandler) AdminCreateEvent(gc *gin.Context) {
151151

152152
txErr := WithTransaction(ctx, h.DbPool, func(tx pgx.Tx) *ApiTxError {
153153
txErr := h.CheckOrganizationAllPermissions(
154-
gc, tx, userId, *payload.OrganizationId, *payload.OrganizationKey,
154+
gc, tx, userId, *payload.OrganizationId,
155155
app.PermChooseAsEventOrganization|app.PermAddEvent)
156156
if txErr != nil {
157157
return txErr
158158
}
159159

160-
// Check if user can create an event in 'incomingEvent.VenueId'
160+
// Check if user can create an event in 'eventPayload.VenueId'
161161
if payload.VenueId != nil {
162162
venuePermissions, err := h.GetUserEffectiveVenuePermissions(gc, tx, userId, *payload.VenueId)
163163
if err != nil {
@@ -184,7 +184,7 @@ func (h *ApiHandler) AdminCreateEvent(gc *gin.Context) {
184184
created_by
185185
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
186186
RETURNING id`
187-
query := strings.Replace(insertEventQuery, "{{schema}}", h.Config.DbSchema, 1)
187+
query := strings.Replace(insertEventQuery, "{{schema}}", h.DbSchema, 1)
188188

189189
err = tx.QueryRow(
190190
ctx, query,
@@ -272,7 +272,7 @@ func (h *ApiHandler) AdminCreateEvent(gc *gin.Context) {
272272
queryTemplate := `
273273
INSERT INTO {{schema}}.event_type_link (event_id, type_id, genre_id)
274274
VALUES ($1, $2, $3)`
275-
query = strings.Replace(queryTemplate, "{{schema}}", h.Config.DbSchema, 1)
275+
query = strings.Replace(queryTemplate, "{{schema}}", h.DbSchema, 1)
276276

277277
for _, pair := range payload.TypeGenrePairs {
278278
_, err := tx.Exec(ctx, query, newEventId, pair.TypeId, pair.GenreId)
@@ -315,8 +315,8 @@ func (h *ApiHandler) AdminCreateEvent(gc *gin.Context) {
315315
apiRequest.SuccessNoData(http.StatusCreated, "")
316316
}
317317

318-
// Validate validates the incomingEvent struct
319-
func (e *incomingEvent) Validate() error {
318+
// Validate validates the eventPayload struct
319+
func (e *eventPayload) Validate() error {
320320
var errs []string
321321

322322
// Validate Title
@@ -512,7 +512,7 @@ func (e *incomingEvent) Validate() error {
512512
return nil
513513
}
514514

515-
func (e *incomingEvent) hasVenue() bool {
515+
func (e *eventPayload) hasVenue() bool {
516516
return e.VenueId != nil
517517
}
518518

@@ -533,14 +533,6 @@ func isBeforeToday(dateStr string) (bool, error) {
533533
return d.Before(today), nil
534534
}
535535

536-
func parseDate(dateStr string) (time.Time, error) {
537-
return time.Parse("2006-01-02", dateStr)
538-
}
539-
540-
func parseTime(timeStr string) (time.Time, error) {
541-
return time.Parse("15:04", timeStr)
542-
}
543-
544536
func validateEventDate(e model.EventDatePayload, index int) []string {
545537
var errs []string
546538

api/admin_create_organization.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package api
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"strings"
7+
8+
"github.com/gin-gonic/gin"
9+
"github.com/jackc/pgx/v5"
10+
"github.com/sndcds/grains/grains_api"
11+
"github.com/sndcds/grains/grains_token"
12+
"github.com/sndcds/uranus/app"
13+
)
14+
15+
func (h *ApiHandler) AdminCreateOrganization(gc *gin.Context) {
16+
ctx := gc.Request.Context()
17+
userId := h.userId(gc)
18+
apiRequest := grains_api.NewRequest(gc, "create-organization")
19+
20+
type Payload struct {
21+
Name string `json:"name" binding:"required"`
22+
}
23+
24+
payload, ok := grains_api.DecodeJSONBody[Payload](gc, apiRequest)
25+
if !ok {
26+
return
27+
}
28+
29+
// Validate name
30+
payload.Name = strings.TrimSpace(payload.Name)
31+
if payload.Name == "" {
32+
apiRequest.Error(http.StatusBadRequest, "organization name cannot be empty")
33+
return
34+
}
35+
apiRequest.Metadata["organization_name"] = payload.Name
36+
37+
apiKey := grains_token.GenerateUuid()
38+
39+
var newOrgId int
40+
txErr := WithTransaction(ctx, h.DbPool, func(tx pgx.Tx) *ApiTxError {
41+
query := fmt.Sprintf(`INSERT INTO %s.organization (name, api_key) VALUES ($1, $2) RETURNING id`, h.DbSchema)
42+
err := tx.QueryRow(ctx, query, payload.Name, apiKey).Scan(&newOrgId)
43+
if err != nil {
44+
return &ApiTxError{
45+
Code: http.StatusInternalServerError,
46+
Err: fmt.Errorf("failed to insert organization: %v", err),
47+
}
48+
}
49+
50+
// Insert user_organization_link
51+
insertLinkQuery := fmt.Sprintf(
52+
`INSERT INTO %s.user_organization_link (user_id, organization_id, permissions) VALUES ($1, $2, $3)`,
53+
h.DbSchema)
54+
_, err = tx.Exec(gc, insertLinkQuery, userId, newOrgId, app.PermCombinationAdmin)
55+
if err != nil {
56+
return &ApiTxError{
57+
Code: http.StatusInternalServerError,
58+
Err: fmt.Errorf("insert user_organization_link failed: %w", err),
59+
}
60+
}
61+
62+
// Insert organization_member_link
63+
insertMemberQuery := fmt.Sprintf(
64+
`INSERT INTO %s.organization_member_link (organization_id, user_id, has_joined) VALUES ($1, $2, $3)`,
65+
h.DbSchema)
66+
_, err = tx.Exec(gc, insertMemberQuery, newOrgId, userId, true)
67+
if err != nil {
68+
return &ApiTxError{
69+
Code: http.StatusInternalServerError,
70+
Err: fmt.Errorf("insert organization_member_link failed: %w", err),
71+
}
72+
}
73+
74+
return nil
75+
})
76+
if txErr != nil {
77+
apiRequest.Error(txErr.Code, txErr.Error())
78+
return
79+
}
80+
81+
// Success response
82+
apiRequest.Metadata["organization_id"] = newOrgId
83+
apiRequest.SuccessNoData(http.StatusCreated, "")
84+
}

api/admin_create_space.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (h *ApiHandler) AdminCreateSpace(gc *gin.Context) {
5050
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
5151
RETURNING id
5252
`
53-
insertSpaceQuery = strings.Replace(insertSpaceQuery, "{{schema}}", h.Config.DbSchema, 1)
53+
insertSpaceQuery = strings.Replace(insertSpaceQuery, "{{schema}}", h.DbSchema, 1)
5454

5555
err = tx.QueryRow(gc, insertSpaceQuery,
5656
req.VenueId,

api/admin_create_venue.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package api
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"strings"
7+
8+
"github.com/gin-gonic/gin"
9+
"github.com/jackc/pgx/v5"
10+
"github.com/sndcds/grains/grains_api"
11+
"github.com/sndcds/uranus/app"
12+
)
13+
14+
func (h *ApiHandler) AdminCreateVenue(gc *gin.Context) {
15+
ctx := gc.Request.Context()
16+
userId := h.userId(gc)
17+
apiRequest := grains_api.NewRequest(gc, "create-venue")
18+
19+
type Payload struct {
20+
OrganizationId int `json:"organization_id" binding:"required"`
21+
VenueName string `json:"venue_name" binding:"required"`
22+
}
23+
payload, ok := grains_api.DecodeJSONBody[Payload](gc, apiRequest)
24+
if !ok {
25+
return
26+
}
27+
28+
venueName := strings.TrimSpace(payload.VenueName)
29+
if venueName == "" {
30+
apiRequest.Error(http.StatusBadRequest, "venue_name cannot be empty")
31+
return
32+
}
33+
34+
apiRequest.Metadata["organization_id"] = payload.OrganizationId
35+
apiRequest.Metadata["venue_name"] = venueName
36+
37+
txErr := WithTransaction(ctx, h.DbPool, func(tx pgx.Tx) *ApiTxError {
38+
39+
txErr := h.CheckOrganizationAllPermissions(
40+
gc, tx, userId, payload.OrganizationId,
41+
app.PermAddVenue)
42+
if txErr != nil {
43+
return txErr
44+
}
45+
46+
newVenueId := -1
47+
query := fmt.Sprintf(`INSERT INTO %s.venue (organization_id, name) VALUES ($1, $2) RETURNING id`, h.DbSchema)
48+
err := tx.QueryRow(ctx, query, payload.OrganizationId, venueName).Scan(&newVenueId)
49+
if err != nil {
50+
return &ApiTxError{
51+
Code: http.StatusInternalServerError,
52+
Err: fmt.Errorf("Internal server error"),
53+
}
54+
}
55+
apiRequest.Metadata["venue_id"] = newVenueId
56+
return nil
57+
})
58+
if txErr != nil {
59+
apiRequest.Error(txErr.Code, txErr.Error())
60+
return
61+
}
62+
63+
apiRequest.SuccessNoData(http.StatusOK, "venue successfully created")
64+
}

api/admin_delete_event.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import (
55
"net/http"
66

77
"github.com/gin-gonic/gin"
8-
"github.com/sndcds/grains/grainsapi"
8+
"github.com/sndcds/grains/grains_api"
99
)
1010

1111
func (h *ApiHandler) AdminDeleteEvent(gc *gin.Context) {
1212
ctx := gc.Request.Context()
1313
userId := h.userId(gc)
14-
apiRequest := grainsapi.NewRequest(gc, "admin-delete-event")
14+
apiRequest := grains_api.NewRequest(gc, "admin-delete-event")
1515

1616
err := h.VerifyUserPassword(gc, userId)
1717
if err != nil {
@@ -26,7 +26,7 @@ func (h *ApiHandler) AdminDeleteEvent(gc *gin.Context) {
2626
}
2727
apiRequest.SetMeta("event_id", eventId)
2828

29-
query := fmt.Sprintf(`DELETE FROM %s.event WHERE id = $1`, h.Config.DbSchema)
29+
query := fmt.Sprintf(`DELETE FROM %s.event WHERE id = $1`, h.DbSchema)
3030
cmdTag, err := h.DbPool.Exec(ctx, query, eventId)
3131
if err != nil {
3232
apiRequest.Error(http.StatusInternalServerError, "failed to delete event")

api/admin_delete_event_date.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import (
55
"net/http"
66

77
"github.com/gin-gonic/gin"
8-
"github.com/sndcds/grains/grainsapi"
8+
"github.com/sndcds/grains/grains_api"
99
)
1010

1111
func (h *ApiHandler) AdminDeleteEventDate(gc *gin.Context) {
1212
ctx := gc.Request.Context()
1313
userId := h.userId(gc)
14-
apiRequest := grainsapi.NewRequest(gc, "admin-delete-event-date")
14+
apiRequest := grains_api.NewRequest(gc, "admin-delete-event-date")
1515

1616
err := h.VerifyUserPassword(gc, userId)
1717
if err != nil {
@@ -33,7 +33,7 @@ func (h *ApiHandler) AdminDeleteEventDate(gc *gin.Context) {
3333
}
3434
apiRequest.SetMeta("event_date_id", eventDateId)
3535

36-
query := fmt.Sprintf(`DELETE FROM %s.event_date WHERE id = $1`, h.Config.DbSchema)
36+
query := fmt.Sprintf(`DELETE FROM %s.event_date WHERE id = $1`, h.DbSchema)
3737
cmdTag, err := h.DbPool.Exec(ctx, query, eventDateId)
3838
if err != nil {
3939
apiRequest.Error(http.StatusInternalServerError, "failed to delete event date")

api/admin_delete_event_image.go

Lines changed: 0 additions & 44 deletions
This file was deleted.

api/admin_delete_organization.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,29 @@ import (
55
"net/http"
66

77
"github.com/gin-gonic/gin"
8-
"github.com/sndcds/grains/grainsapi"
8+
"github.com/sndcds/grains/grains_api"
99
)
1010

1111
func (h *ApiHandler) AdminDeleteOrganization(gc *gin.Context) {
1212
ctx := gc.Request.Context()
1313
userId := h.userId(gc)
14-
apiRequest := grainsapi.NewRequest(gc, "admin-delete-organization")
14+
apiRequest := grains_api.NewRequest(gc, "admin-delete-organization")
1515

1616
err := h.VerifyUserPassword(gc, userId)
1717
if err != nil {
1818
apiRequest.Error(http.StatusUnauthorized, err.Error())
1919
return
2020
}
2121

22-
orgId, ok := ParamInt(gc, "organizationId")
22+
organizationId, ok := ParamInt(gc, "organizationId")
2323
if !ok {
2424
apiRequest.Error(http.StatusBadRequest, "invalid organizationId")
2525
return
2626
}
27-
apiRequest.SetMeta("organization_id", orgId)
27+
apiRequest.SetMeta("organization_id", organizationId)
2828

29-
query := fmt.Sprintf(`DELETE FROM %s.organization WHERE id = $1`, h.Config.DbSchema)
30-
cmdTag, err := h.DbPool.Exec(ctx, query, orgId)
29+
query := fmt.Sprintf(`DELETE FROM %s.organization WHERE id = $1`, h.DbSchema)
30+
cmdTag, err := h.DbPool.Exec(ctx, query, organizationId)
3131
if err != nil {
3232
apiRequest.Error(http.StatusInternalServerError, "failed to delete organization")
3333
return

0 commit comments

Comments
 (0)