feat: update logic for update query to db for posts
This commit is contained in:
@@ -455,6 +455,9 @@ const docTemplate = `{
|
|||||||
"id": {
|
"id": {
|
||||||
"type": "integer"
|
"type": "integer"
|
||||||
},
|
},
|
||||||
|
"published": {
|
||||||
|
"type": "boolean"
|
||||||
|
},
|
||||||
"title": {
|
"title": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
}
|
}
|
||||||
@@ -466,6 +469,9 @@ const docTemplate = `{
|
|||||||
"content": {
|
"content": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
},
|
},
|
||||||
|
"published": {
|
||||||
|
"type": "boolean"
|
||||||
|
},
|
||||||
"title": {
|
"title": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -444,6 +444,9 @@
|
|||||||
"id": {
|
"id": {
|
||||||
"type": "integer"
|
"type": "integer"
|
||||||
},
|
},
|
||||||
|
"published": {
|
||||||
|
"type": "boolean"
|
||||||
|
},
|
||||||
"title": {
|
"title": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
}
|
}
|
||||||
@@ -455,6 +458,9 @@
|
|||||||
"content": {
|
"content": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
},
|
},
|
||||||
|
"published": {
|
||||||
|
"type": "boolean"
|
||||||
|
},
|
||||||
"title": {
|
"title": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,6 +25,8 @@ definitions:
|
|||||||
type: string
|
type: string
|
||||||
id:
|
id:
|
||||||
type: integer
|
type: integer
|
||||||
|
published:
|
||||||
|
type: boolean
|
||||||
title:
|
title:
|
||||||
type: string
|
type: string
|
||||||
type: object
|
type: object
|
||||||
@@ -32,6 +34,8 @@ definitions:
|
|||||||
properties:
|
properties:
|
||||||
content:
|
content:
|
||||||
type: string
|
type: string
|
||||||
|
published:
|
||||||
|
type: boolean
|
||||||
title:
|
title:
|
||||||
type: string
|
type: string
|
||||||
type: object
|
type: object
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package repositories
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/logger"
|
"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/logger"
|
||||||
"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/storage"
|
"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/storage"
|
||||||
@@ -78,17 +79,29 @@ func (p *postRepository) Create(ctx context.Context, post storage.Post) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *postRepository) Update(ctx context.Context, id int, post storage.Post) error {
|
func (p *postRepository) Update(ctx context.Context, id int, post storage.Post) error {
|
||||||
_, err := p.db.Exec(
|
query := "UPDATE posts SET "
|
||||||
"UPDATE posts SET title = ?, content = ? WHERE id = ?",
|
args := []interface{}{}
|
||||||
post.Title,
|
updates := []string{}
|
||||||
post.Content,
|
|
||||||
id,
|
if post.Title != "" {
|
||||||
)
|
updates = append(updates, "title = ?")
|
||||||
if err != nil {
|
args = append(args, post.Title)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
p.logger.Info("Post updated:", "id", id)
|
|
||||||
return nil
|
if post.Content != "" {
|
||||||
|
updates = append(updates, "content = ?")
|
||||||
|
args = append(args, post.Content)
|
||||||
|
}
|
||||||
|
|
||||||
|
updates = append(updates, "published = ?")
|
||||||
|
args = append(args, post.Published)
|
||||||
|
updates = append(updates, "updated_at = CURRENT_TIMESTAMP")
|
||||||
|
query += strings.Join(updates, ", ")
|
||||||
|
query += " WHERE id = ?"
|
||||||
|
args = append(args, id)
|
||||||
|
|
||||||
|
_, err := p.db.ExecContext(ctx, query, args...)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *postRepository) Delete(ctx context.Context, id int) error {
|
func (p *postRepository) Delete(ctx context.Context, id int) error {
|
||||||
@@ -122,5 +135,3 @@ func (p *postRepository) IsExist(ctx context.Context, id int) bool {
|
|||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Add query for change published status
|
|
||||||
|
|||||||
Reference in New Issue
Block a user