Skip to content

Commit 03dce7e

Browse files
committed
added db queries for the event
1 parent af786b1 commit 03dce7e

File tree

4 files changed

+102
-19
lines changed

4 files changed

+102
-19
lines changed

server/cmd/api/events.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func (app *application) createEvent(c *gin.Context) {
2020
return
2121
}
2222

23-
err := app.models.Events.insert(event)
23+
err := app.models.Events.Insert(&event)
2424
if err != nil {
2525
c.JSON(http.StatusInternalServerError, gin.H{
2626
"error": "Failed to create event",
@@ -53,14 +53,14 @@ func (app *application) getEvent(c *gin.Context) {
5353
}
5454

5555
event, err := app.models.Events.Get(id)
56-
if err == nil {
56+
if err != nil {
5757
c.JSON(http.StatusBadRequest, gin.H{
5858
"error": "Event not found",
5959
})
6060
return
6161
}
6262

63-
if err != nil {
63+
if event == nil {
6464
c.JSON(http.StatusInternalServerError, gin.H{
6565
"error": "Failed to retireve event",
6666
})

server/go.mod

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ require (
1515
github.com/go-playground/universal-translator v0.18.1 // indirect
1616
github.com/go-playground/validator/v10 v10.20.0 // indirect
1717
github.com/goccy/go-json v0.10.2 // indirect
18+
github.com/google/go-cmp v0.6.0 // indirect
1819
github.com/json-iterator/go v1.1.12 // indirect
1920
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
2021
github.com/leodido/go-urn v1.4.0 // indirect
@@ -25,10 +26,10 @@ require (
2526
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
2627
github.com/ugorji/go/codec v1.2.12 // indirect
2728
golang.org/x/arch v0.8.0 // indirect
28-
golang.org/x/crypto v0.36.0 // indirect
29-
golang.org/x/net v0.38.0 // indirect
30-
golang.org/x/sys v0.31.0 // indirect
31-
golang.org/x/text v0.23.0 // indirect
29+
golang.org/x/crypto v0.40.0 // indirect
30+
golang.org/x/net v0.42.0 // indirect
31+
golang.org/x/sys v0.34.0 // indirect
32+
golang.org/x/text v0.27.0 // indirect
3233
google.golang.org/protobuf v1.34.2 // indirect
3334
gopkg.in/yaml.v3 v3.0.1 // indirect
3435
)

server/go.sum

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
2727
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
2828
github.com/golang-migrate/migrate/v4 v4.18.3 h1:EYGkoOsvgHHfm5U/naS1RP/6PL/Xv3S4B/swMiAmDLs=
2929
github.com/golang-migrate/migrate/v4 v4.18.3/go.mod h1:99BKpIi6ruaaXRM1A77eqZ+FWPQ3cfRa+ZVy5bmWMaY=
30-
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
31-
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
30+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
31+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
3232
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
3333
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
3434
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
@@ -81,18 +81,16 @@ go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0
8181
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
8282
golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
8383
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
84-
golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34=
85-
golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc=
86-
golang.org/x/net v0.38.0 h1:vRMAPTMaeGqVhG5QyLJHqNDwecKTomGeqbnfZyKlBI8=
87-
golang.org/x/net v0.38.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
84+
golang.org/x/crypto v0.40.0 h1:r4x+VvoG5Fm+eJcxMaY8CQM7Lb0l1lsmjGBQ6s8BfKM=
85+
golang.org/x/crypto v0.40.0/go.mod h1:Qr1vMER5WyS2dfPHAlsOj01wgLbsyWtFn/aY+5+ZdxY=
86+
golang.org/x/net v0.42.0 h1:jzkYrhi3YQWD6MLBJcsklgQsoAcw89EcZbJw8Z614hs=
87+
golang.org/x/net v0.42.0/go.mod h1:FF1RA5d3u7nAYA4z2TkclSCKh68eSXtiFwcWQpPXdt8=
8888
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
8989
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
90-
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
91-
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
92-
golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY=
93-
golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4=
94-
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 h1:+cNy6SZtPcJQH3LJVLOSmiC7MMxXNOb3PU/VUEz+EhU=
95-
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028/go.mod h1:NDW/Ps6MPRej6fsCIbMTohpP40sJ/P/vI1MoTEGwX90=
90+
golang.org/x/sys v0.34.0 h1:H5Y5sJ2L2JRdyv7ROF1he/lPdvFsd0mJHFw2ThKHxLA=
91+
golang.org/x/sys v0.34.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
92+
golang.org/x/text v0.27.0 h1:4fGWRpyh641NLlecmyl4LOe6yDdfaYNrGb2zdfo4JV4=
93+
golang.org/x/text v0.27.0/go.mod h1:1D28KMCvyooCX9hBiosv5Tz/+YLxj0j7XhWjpSUF7CU=
9694
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
9795
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
9896
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=

server/internals/database/event.go

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

33
import (
4+
"context"
45
"database/sql"
56
"time"
67
)
@@ -17,3 +18,86 @@ type Event struct {
1718
Date time.Time `json:"date" binding:"required" datetime:"2003-02-12"`
1819
Location string `json:"location" binding:"required, min=3"`
1920
}
21+
22+
func (e *EventModel) Insert(event *Event) error {
23+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
24+
defer cancel()
25+
26+
query := `INSERT INTO events(owner_id, name, description, date, location) VALUES($1, $2, $3, $4, $5)`
27+
28+
return e.Db.QueryRowContext(ctx, query, event.OwnerId, event.Name, event.Description, event.Date, event.Location).Scan(&event.Id)
29+
}
30+
31+
func (e *EventModel) GetAll() ([]*Event, error) {
32+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
33+
defer cancel()
34+
35+
query := `SELECT * FROM events`
36+
37+
rows, err := e.Db.QueryContext(ctx, query)
38+
if err != nil {
39+
return nil, err
40+
}
41+
42+
defer rows.Close()
43+
44+
events := []*Event{}
45+
46+
for rows.Next() {
47+
var event Event
48+
err := rows.Scan(&event.Id, &event.OwnerId, &event.Name, &event.Description, &event.Date, &event.Location)
49+
if err != nil {
50+
return nil, err
51+
}
52+
events = append(events, &event)
53+
}
54+
55+
if err := rows.Err(); err != nil {
56+
return nil, err
57+
}
58+
59+
return events, nil
60+
}
61+
62+
func (e *EventModel) Get(id int) (*Event, error) {
63+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
64+
defer cancel()
65+
66+
query := `SELECT * FROM events WHERE id = $1`
67+
var event Event
68+
69+
err := e.Db.QueryRowContext(ctx, query, id).Scan(&event, id, &event.OwnerId, &event.Name, &event.Description, &event.Date, &event.Location)
70+
if err != nil {
71+
if err == sql.ErrNoRows {
72+
return nil, nil
73+
}
74+
return nil, err
75+
}
76+
return &event, nil
77+
}
78+
79+
func (e *EventModel) Update(event *Event) error {
80+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
81+
defer cancel()
82+
83+
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)
85+
if err != nil {
86+
return err
87+
}
88+
89+
return nil
90+
}
91+
92+
func (e *EventModel) Delete(id int) error {
93+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
94+
defer cancel()
95+
96+
query := `DELETE FROM events WHERE id = $1`
97+
_, err := e.Db.ExecContext(ctx, query, id)
98+
if err != nil {
99+
return err
100+
}
101+
102+
return nil
103+
}

0 commit comments

Comments
 (0)