Skip to content

Commit 5c920c5

Browse files
committed
fix: Get, GetAll handler
1 parent 765fdc8 commit 5c920c5

File tree

6 files changed

+34
-14
lines changed

6 files changed

+34
-14
lines changed

server/cmd/api/events.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"fmt"
45
"net/http"
56
"strconv"
67

@@ -14,14 +15,18 @@ func (app *application) createEvent(c *gin.Context) {
1415
var event database.Event
1516

1617
if err := c.ShouldBindJSON(&event); err != nil {
18+
fmt.Println("Bind error:", err)
1719
c.JSON(http.StatusBadRequest, gin.H{
1820
"error": err.Error(),
1921
})
2022
return
2123
}
2224

25+
// Insert into db
2326
err := app.models.Events.Insert(&event)
27+
2428
if err != nil {
29+
fmt.Println("DB insert error:", err)
2530
c.JSON(http.StatusInternalServerError, gin.H{
2631
"error": "Failed to create event",
2732
})
@@ -82,7 +87,7 @@ func (app *application) updateEvent(c *gin.Context) {
8287
existingEvent, err := app.models.Events.Get(id)
8388
if err != nil {
8489
c.JSON(http.StatusInternalServerError, gin.H{
85-
"error": "Failed to retireve event",
90+
"error": "Failed to retireve event " + err.Error(),
8691
})
8792
return
8893
}
@@ -96,10 +101,11 @@ func (app *application) updateEvent(c *gin.Context) {
96101

97102
updatedEvent := &database.Event{}
98103

99-
if err := c.ShouldBind(updatedEvent); err != nil {
104+
if err := c.ShouldBindJSON(updatedEvent); err != nil {
100105
c.JSON(http.StatusBadGateway, gin.H{
101106
"error": err.Error(),
102107
})
108+
return
103109
}
104110

105111
updatedEvent.Id = id
@@ -110,6 +116,9 @@ func (app *application) updateEvent(c *gin.Context) {
110116
})
111117
return
112118
}
119+
120+
// Return the updated event
121+
c.JSON(http.StatusOK, updatedEvent)
113122
}
114123

115124
func (app *application) deleteEvent(c *gin.Context) {

server/cmd/api/routes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func (app *application) routes() http.Handler {
1717
v1.PUT("/events/:id", app.updateEvent) // update event by id
1818
v1.DELETE("/events/:id", app.deleteEvent) //delete the event by id
1919

20-
v1.POST("/register", app.registerUser) // register user
20+
v1.POST("/auth/register", app.registerUser) // register user
2121
}
2222

2323
return r

server/data.db

0 Bytes
Binary file not shown.

server/internals/database/event.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package database
33
import (
44
"context"
55
"database/sql"
6+
"fmt"
67
"time"
78
)
89

@@ -11,21 +12,26 @@ type EventModel struct {
1112
}
1213

1314
type Event struct {
14-
Id int `json:"id"`
15-
OwnerId int `json:"ownerId" binding:"required"`
16-
Name string `json:"name" binding:"required, min=3, max=50"`
17-
Description string `json:"description" binding:"required, min=3, max=500"`
18-
Date time.Time `json:"date" binding:"required" datetime:"2003-02-12"`
19-
Location string `json:"location" binding:"required, min=3"`
15+
Id int `json:"id"`
16+
OwnerId int `json:"ownerId" binding:"required"`
17+
Name string `json:"name" binding:"required,min=3,max=50"`
18+
Description string `json:"description" binding:"required,min=3,max=500"`
19+
Date string `json:"date" binding:"required"`
20+
Location string `json:"location" binding:"required,min=3"`
2021
}
2122

2223
func (e *EventModel) Insert(event *Event) error {
2324
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
2425
defer cancel()
2526

26-
query := `INSERT INTO events(owner_id, name, description, date, location) VALUES($1, $2, $3, $4, $5)`
27+
parsedDate, err := time.Parse("2006-01-02", event.Date)
28+
if err != nil {
29+
return fmt.Errorf("invalid date format: %w", err)
30+
}
31+
32+
query := `INSERT INTO events(owner_id, name, description, date, location) VALUES($1, $2, $3, $4, $5) RETURNING id`
2733

28-
return e.Db.QueryRowContext(ctx, query, event.OwnerId, event.Name, event.Description, event.Date, event.Location).Scan(&event.Id)
34+
return e.Db.QueryRowContext(ctx, query, event.OwnerId, event.Name, event.Description, parsedDate, event.Location).Scan(&event.Id)
2935
}
3036

3137
func (e *EventModel) GetAll() ([]*Event, error) {
@@ -66,7 +72,7 @@ func (e *EventModel) Get(id int) (*Event, error) {
6672
query := `SELECT * FROM events WHERE id = $1`
6773
var event Event
6874

69-
err := e.Db.QueryRowContext(ctx, query, id).Scan(&event, id, &event.OwnerId, &event.Name, &event.Description, &event.Date, &event.Location)
75+
err := e.Db.QueryRowContext(ctx, query, id).Scan(&event.Id, &event.OwnerId, &event.Name, &event.Description, &event.Date, &event.Location)
7076
if err != nil {
7177
if err == sql.ErrNoRows {
7278
return nil, nil
@@ -80,8 +86,13 @@ func (e *EventModel) Update(event *Event) error {
8086
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
8187
defer cancel()
8288

89+
parsedDate, err := time.Parse("2006-01-02", event.Date)
90+
if err != nil {
91+
return fmt.Errorf("invalid date format: %w", err)
92+
}
93+
8394
query := `UPDATE events SET name = $2, description = $3, date = $4, location = $5 WHERE id = $1`
84-
_, err := e.Db.ExecContext(ctx, query, event.Id, event.Name, event.Description, event.Date, event.Location)
95+
_, err = e.Db.ExecContext(ctx, query, event.Id, event.Name, event.Description, parsedDate, event.Location)
8596
if err != nil {
8697
return err
8798
}

server/tmp/build-errors.log

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1
1+
exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1

server/tmp/main

1.05 MB
Binary file not shown.

0 commit comments

Comments
 (0)