chore: add code for 2 handlers with GET
Some checks failed
Backend ci / build (push) Failing after 36m27s

This commit is contained in:
d3m0k1d
2026-02-03 19:41:18 +03:00
parent 440b836d54
commit 44406b02d3
4 changed files with 43 additions and 9 deletions

View File

@@ -13,7 +13,7 @@ var log = logger.New(false)
// @Tags posts // @Tags posts
// @Accept json // @Accept json
// @Produce json // @Produce json
// @Success 200 {object} []storage.Post // @Success 200 {object} []storage.PostReq
// @Router /api/v1/posts [get] // @Router /api/v1/posts [get]
func GetPosts(c *gin.Context) { func GetPosts(c *gin.Context) {
log.Info("GetPosts") log.Info("GetPosts")
@@ -25,7 +25,7 @@ func GetPosts(c *gin.Context) {
// @Tags posts // @Tags posts
// @Accept json // @Accept json
// @Produce json // @Produce json
// @Success 200 {object} storage.Post // @Success 200 {object} storage.PostReq
// @Router /api/v1/posts/{id} [get] // @Router /api/v1/posts/{id} [get]
func GetPost(c *gin.Context) { func GetPost(c *gin.Context) {
log.Info("GetPost") log.Info("GetPost")

View File

@@ -7,8 +7,8 @@ import (
) )
type PostRepository interface { type PostRepository interface {
GetAll(ctx context.Context) ([]storage.Post, error) GetAll(ctx context.Context) ([]storage.PostReq, error)
GetByID(ctx context.Context, id int) (storage.Post, error) GetByID(ctx context.Context, id int) (storage.PostReq, error)
Create(ctx context.Context, post storage.Post) error Create(ctx context.Context, post storage.Post) error
Update(ctx context.Context, id int, post storage.Post) error Update(ctx context.Context, id int, post storage.Post) error
Delete(ctx context.Context, id int) error Delete(ctx context.Context, id int) error

View File

@@ -19,13 +19,42 @@ func NewPostRepository(db *sql.DB) PostRepository {
logger: logger.New(false), logger: logger.New(false),
} }
} }
func (p *postRepository) GetAll(ctx context.Context) ([]storage.Post, error) { func (p *postRepository) GetAll(ctx context.Context) ([]storage.PostReq, error) {
return nil, nil var result []storage.PostReq
rows, err := p.db.Query("SELECT title content FROM posts")
if err != nil {
p.logger.Error(err.Error())
return nil, err
}
for rows.Next() {
var title string
var content string
err := rows.Scan(&title, &content)
if err != nil {
p.logger.Error("error scan: " + err.Error())
}
result = append(result, storage.PostReq{
Title: title,
Content: content,
})
}
return result, nil
} }
func (p *postRepository) GetByID(ctx context.Context, id int) (storage.Post, error) { func (p *postRepository) GetByID(ctx context.Context, id int) (storage.PostReq, error) {
var result storage.PostReq
return storage.Post{}, nil row := p.db.QueryRow("SELECT title content FROM posts WHERE id = ?", id)
var title string
var content string
err := row.Scan(&title, &content)
if err != nil {
p.logger.Error("error scan: " + err.Error())
}
result = storage.PostReq{
Title: title,
Content: content,
}
return result, nil
} }
func (p *postRepository) Create(ctx context.Context, post storage.Post) error { func (p *postRepository) Create(ctx context.Context, post storage.Post) error {

View File

@@ -6,3 +6,8 @@ type Post struct {
Content string `db:"content"` Content string `db:"content"`
CreatedAt string `db:"created_at"` CreatedAt string `db:"created_at"`
} }
type PostReq struct {
Title string `json:"title"`
Content string `json:"content"`
}