develop merge 3 #4
@@ -13,7 +13,7 @@ var log = logger.New(false)
|
||||
// @Tags posts
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} []storage.Post
|
||||
// @Success 200 {object} []storage.PostReq
|
||||
// @Router /api/v1/posts [get]
|
||||
func GetPosts(c *gin.Context) {
|
||||
log.Info("GetPosts")
|
||||
@@ -25,7 +25,7 @@ func GetPosts(c *gin.Context) {
|
||||
// @Tags posts
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} storage.Post
|
||||
// @Success 200 {object} storage.PostReq
|
||||
// @Router /api/v1/posts/{id} [get]
|
||||
func GetPost(c *gin.Context) {
|
||||
log.Info("GetPost")
|
||||
|
||||
@@ -7,8 +7,8 @@ import (
|
||||
)
|
||||
|
||||
type PostRepository interface {
|
||||
GetAll(ctx context.Context) ([]storage.Post, error)
|
||||
GetByID(ctx context.Context, id int) (storage.Post, error)
|
||||
GetAll(ctx context.Context) ([]storage.PostReq, error)
|
||||
GetByID(ctx context.Context, id int) (storage.PostReq, error)
|
||||
Create(ctx context.Context, post storage.Post) error
|
||||
Update(ctx context.Context, id int, post storage.Post) error
|
||||
Delete(ctx context.Context, id int) error
|
||||
|
||||
@@ -19,13 +19,42 @@ func NewPostRepository(db *sql.DB) PostRepository {
|
||||
logger: logger.New(false),
|
||||
}
|
||||
}
|
||||
func (p *postRepository) GetAll(ctx context.Context) ([]storage.Post, error) {
|
||||
return nil, nil
|
||||
func (p *postRepository) GetAll(ctx context.Context) ([]storage.PostReq, error) {
|
||||
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) {
|
||||
|
||||
return storage.Post{}, nil
|
||||
func (p *postRepository) GetByID(ctx context.Context, id int) (storage.PostReq, error) {
|
||||
var result storage.PostReq
|
||||
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 {
|
||||
|
||||
@@ -6,3 +6,8 @@ type Post struct {
|
||||
Content string `db:"content"`
|
||||
CreatedAt string `db:"created_at"`
|
||||
}
|
||||
|
||||
type PostReq struct {
|
||||
Title string `json:"title"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user