Skip to content

Commit fad40b9

Browse files
AstanaMacAstanaMac
authored andcommitted
blogging-platform
1 parent cb75c49 commit fad40b9

File tree

8 files changed

+278
-43
lines changed

8 files changed

+278
-43
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,4 @@ SKIPTEST=optional go test -v -bench=. -race ./exercise1/...
6060

6161
## PR
6262

63-
create PR from your repo to this one from branch `{firstname}-{lastname}` for example `talgat-saribayev`.
63+
create PR from your repo to this one from branch `{firstname}-{lastname}` for example `talgat-saribayev`.

exercise7/blogging-platform/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
# Blogging Platform
22

3-
Please check https://roadmap.sh/projects/blogging-platform-api.
3+
Project <https://roadmap.sh/projects/blogging-platform-api>
4+
5+
Please check https://roadmap.sh/projects/blogging-platform-api.

exercise7/blogging-platform/go.mod

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,36 @@ module github.com/talgat-ruby/exercises-go/exercise7/blogging-platform
22

33
go 1.23.3
44

5-
require github.com/lib/pq v1.10.9
5+
require (
6+
github.com/gin-gonic/gin v1.10.0
7+
github.com/lib/pq v1.10.9
8+
)
9+
10+
require (
11+
github.com/bytedance/sonic v1.11.6 // indirect
12+
github.com/bytedance/sonic/loader v0.1.1 // indirect
13+
github.com/cloudwego/base64x v0.1.4 // indirect
14+
github.com/cloudwego/iasm v0.2.0 // indirect
15+
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
16+
github.com/gin-contrib/sse v0.1.0 // indirect
17+
github.com/go-playground/locales v0.14.1 // indirect
18+
github.com/go-playground/universal-translator v0.18.1 // indirect
19+
github.com/go-playground/validator/v10 v10.20.0 // indirect
20+
github.com/goccy/go-json v0.10.2 // indirect
21+
github.com/json-iterator/go v1.1.12 // indirect
22+
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
23+
github.com/leodido/go-urn v1.4.0 // indirect
24+
github.com/mattn/go-isatty v0.0.20 // indirect
25+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
26+
github.com/modern-go/reflect2 v1.0.2 // indirect
27+
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
28+
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
29+
github.com/ugorji/go/codec v1.2.12 // indirect
30+
golang.org/x/arch v0.8.0 // indirect
31+
golang.org/x/crypto v0.23.0 // indirect
32+
golang.org/x/net v0.25.0 // indirect
33+
golang.org/x/sys v0.20.0 // indirect
34+
golang.org/x/text v0.15.0 // indirect
35+
google.golang.org/protobuf v1.34.1 // indirect
36+
gopkg.in/yaml.v3 v3.0.1 // indirect
37+
)

exercise7/blogging-platform/go.sum

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,91 @@
1+
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
2+
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
3+
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
4+
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
5+
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
6+
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
7+
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
8+
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
9+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
10+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
11+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
12+
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
13+
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
14+
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
15+
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
16+
github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
17+
github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y=
18+
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
19+
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
20+
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
21+
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
22+
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
23+
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
24+
github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8=
25+
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
26+
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
27+
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
28+
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
29+
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
30+
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
31+
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
32+
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
33+
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
34+
github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM=
35+
github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
36+
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
37+
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
38+
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
139
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
240
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
41+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
42+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
43+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
44+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
45+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
46+
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
47+
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
48+
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
49+
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
50+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
51+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
52+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
53+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
54+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
55+
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
56+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
57+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
58+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
59+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
60+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
61+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
62+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
63+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
64+
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
65+
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
66+
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
67+
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
68+
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
69+
golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
70+
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
71+
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
72+
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
73+
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
74+
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
75+
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
76+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
77+
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
78+
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
79+
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
80+
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
81+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
82+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
83+
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
84+
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
85+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
86+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
87+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
88+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
89+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
90+
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
91+
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=

exercise7/blogging-platform/main.go

Lines changed: 146 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,156 @@
11
package main
22

33
import (
4-
"context"
5-
"log/slog"
6-
"os"
7-
"os/signal"
4+
"database/sql"
5+
"fmt"
6+
"log"
7+
"net/http"
8+
"time"
89

9-
"github.com/talgat-ruby/exercises-go/exercise7/blogging-platform/internal/api"
10-
"github.com/talgat-ruby/exercises-go/exercise7/blogging-platform/internal/db"
10+
"github.com/gin-gonic/gin"
11+
"github.com/lib/pq"
12+
_ "github.com/lib/pq"
1113
)
1214

15+
type BlogPost struct {
16+
ID int `json:"id"`
17+
Title string `json:"title" binding:"required"`
18+
Content string `json:"content" binding:"required"`
19+
Category string `json:"category" binding:"required"`
20+
Tags []string `json:"tags" binding:"required"`
21+
CreatedAt time.Time `json:"createdAt"`
22+
UpdatedAt time.Time `json:"updatedAt"`
23+
}
24+
25+
var db *sql.DB
26+
1327
func main() {
14-
ctx, cancel := context.WithCancel(context.Background())
28+
var err error
29+
connStr := "postgres://astanamac:adlet@localhost:5432/blog?sslmode=disable"
30+
db, err = sql.Open("postgres", connStr)
31+
if err != nil {
32+
log.Fatal("Ошибка при подключении к PostgreSQL:", err)
33+
}
1534

16-
// db
17-
_, err := db.New()
35+
// Проверка соединения
36+
err = db.Ping()
1837
if err != nil {
19-
slog.ErrorContext(
20-
ctx,
21-
"initialize service error",
22-
"service", "db",
23-
"error", err,
24-
)
25-
panic(err)
26-
}
27-
28-
// api
29-
a := api.New()
30-
if err := a.Start(ctx); err != nil {
31-
slog.ErrorContext(
32-
ctx,
33-
"initialize service error",
34-
"service", "api",
35-
"error", err,
36-
)
37-
panic(err)
38-
}
39-
40-
go func() {
41-
shutdown := make(chan os.Signal, 1) // Create channel to signify s signal being sent
42-
signal.Notify(shutdown, os.Interrupt) // When an interrupt is sent, notify the channel
43-
44-
sig := <-shutdown
45-
slog.WarnContext(ctx, "signal received - shutting down...", "signal", sig)
46-
47-
cancel()
48-
}()
38+
log.Fatal("Не удалось подключиться к базе данных:", err)
39+
} else {
40+
fmt.Println("✅ Успешное подключение к базе данных!")
41+
}
42+
43+
r := gin.Default()
44+
r.POST("/posts", createPost)
45+
r.PUT("/posts/:id", updatePost)
46+
r.DELETE("/posts/:id", deletePost)
47+
r.GET("/posts/:id", getPost)
48+
r.GET("/posts", getAllPosts)
49+
50+
fmt.Println("🚀 Сервер запущен на порту 8080")
51+
r.Run(":8080")
52+
}
53+
54+
func createPost(c *gin.Context) {
55+
var post BlogPost
56+
if err := c.ShouldBindJSON(&post); err != nil {
57+
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
58+
return
59+
}
60+
timeNow := time.Now()
61+
post.CreatedAt, post.UpdatedAt = timeNow, timeNow
62+
63+
query := `INSERT INTO posts (title, content, category, tags, created_at, updated_at) VALUES ($1, $2, $3, $4, $5, $6) RETURNING id`
64+
err := db.QueryRow(query, post.Title, post.Content, post.Category, pq.Array(post.Tags), post.CreatedAt, post.UpdatedAt).Scan(&post.ID)
65+
if err != nil {
66+
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
67+
return
68+
}
69+
70+
c.JSON(http.StatusCreated, post)
71+
}
72+
73+
func updatePost(c *gin.Context) {
74+
id := c.Param("id")
75+
var post BlogPost
76+
if err := c.ShouldBindJSON(&post); err != nil {
77+
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
78+
return
79+
}
80+
81+
post.UpdatedAt = time.Now()
82+
query := `UPDATE posts SET title=$1, content=$2, category=$3, tags=$4, updated_at=$5 WHERE id=$6`
83+
result, err := db.Exec(query, post.Title, post.Content, post.Category, pq.Array(post.Tags), post.UpdatedAt, id)
84+
if err != nil {
85+
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
86+
return
87+
}
88+
89+
rowsAffected, _ := result.RowsAffected()
90+
if rowsAffected == 0 {
91+
c.JSON(http.StatusNotFound, gin.H{"error": "Post not found"})
92+
return
93+
}
94+
95+
c.JSON(http.StatusOK, post)
96+
}
97+
98+
func deletePost(c *gin.Context) {
99+
id := c.Param("id")
100+
query := `DELETE FROM posts WHERE id=$1`
101+
result, err := db.Exec(query, id)
102+
if err != nil {
103+
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
104+
return
105+
}
106+
107+
rowsAffected, _ := result.RowsAffected()
108+
if rowsAffected == 0 {
109+
c.JSON(http.StatusNotFound, gin.H{"error": "Post not found"})
110+
return
111+
}
112+
113+
c.Status(http.StatusNoContent)
114+
}
115+
116+
func getPost(c *gin.Context) {
117+
id := c.Param("id")
118+
var post BlogPost
119+
var tags pq.StringArray
120+
121+
query := `SELECT id, title, content, category, tags, created_at, updated_at FROM posts WHERE id=$1`
122+
err := db.QueryRow(query, id).Scan(&post.ID, &post.Title, &post.Content, &post.Category, (*pq.StringArray)(&tags), &post.CreatedAt, &post.UpdatedAt)
123+
if err != nil {
124+
c.JSON(http.StatusNotFound, gin.H{"error": "Post not found"})
125+
return
126+
}
127+
128+
post.Tags = tags
129+
c.JSON(http.StatusOK, post)
130+
}
131+
132+
func getAllPosts(c *gin.Context) {
133+
searchTerm := c.Query("term")
134+
query := `SELECT id, title, content, category, tags, created_at, updated_at FROM posts WHERE title ILIKE $1 OR content ILIKE $1 OR category ILIKE $1`
135+
rows, err := db.Query(query, "%"+searchTerm+"%")
136+
if err != nil {
137+
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
138+
return
139+
}
140+
defer rows.Close()
141+
142+
var posts []BlogPost
143+
for rows.Next() {
144+
var post BlogPost
145+
var tags pq.StringArray
146+
147+
if err := rows.Scan(&post.ID, &post.Title, &post.Content, &post.Category, (*pq.StringArray)(&tags), &post.CreatedAt, &post.UpdatedAt); err != nil {
148+
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
149+
return
150+
}
151+
post.Tags = tags
152+
posts = append(posts, post)
153+
}
154+
155+
c.JSON(http.StatusOK, posts)
49156
}

exercise7/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module exercise7
2+
3+
go 1.24.0

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github.com/talgat-ruby/exercises-go
22

3-
go 1.23
3+
go 1.23.0
4+
5+
toolchain go1.24.0

go.sum

Whitespace-only changes.

0 commit comments

Comments
 (0)