feat: full redy blog and admin panel

This commit is contained in:
d3m0k1d
2026-02-15 16:34:14 +03:00
parent 8d6225f136
commit 7f8d8373a9
18 changed files with 3236 additions and 39 deletions

View File

@@ -22,7 +22,7 @@ func NewPostRepository(db *sql.DB) PostRepository {
}
func (p *postRepository) GetAll(ctx context.Context) ([]storage.PostReq, error) {
var result []storage.PostReq
rows, err := p.db.Query("SELECT id, title, content FROM posts WHERE published = 1")
rows, err := p.db.Query("SELECT id, title, content, CREATED_AT FROM posts WHERE published = 1")
if err != nil {
p.logger.Error(err.Error())
return nil, err
@@ -31,14 +31,16 @@ func (p *postRepository) GetAll(ctx context.Context) ([]storage.PostReq, error)
var title string
var content string
var id int
err := rows.Scan(&id, &title, &content)
var createdAt string
err := rows.Scan(&id, &title, &content, &createdAt)
if err != nil {
p.logger.Error("error scan: " + err.Error())
}
result = append(result, storage.PostReq{
ID: id,
Title: title,
Content: content,
ID: id,
Title: title,
Content: content,
CreatedAt: createdAt,
})
}
return result, nil
@@ -46,17 +48,19 @@ func (p *postRepository) GetAll(ctx context.Context) ([]storage.PostReq, error)
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 = ? AND published = 1", id)
row := p.db.QueryRow("SELECT title, content, CREATED_AT FROM posts WHERE id = ? AND published = 1", id)
var title string
var content string
err := row.Scan(&title, &content)
var createdAt string
err := row.Scan(&title, &content, &createdAt)
if err != nil {
p.logger.Error("error scan: " + err.Error())
}
result = storage.PostReq{
ID: id,
Title: title,
Content: content,
ID: id,
Title: title,
Content: content,
CreatedAt: createdAt,
}
return result, nil
}
@@ -135,3 +139,27 @@ func (p *postRepository) IsExist(ctx context.Context, id int) bool {
}
return true
}
func (p *postRepository) GetAllAdmin(ctx context.Context) ([]storage.PostReq, error) {
result := []storage.PostReq{}
rows, err := p.db.Query("SELECT id, title, content FROM posts")
if err != nil {
p.logger.Error(err.Error())
return nil, err
}
for rows.Next() {
var title string
var content string
var id int
err := rows.Scan(&id, &title, &content)
if err != nil {
p.logger.Error("error scan: " + err.Error())
}
result = append(result, storage.PostReq{
ID: id,
Title: title,
Content: content,
})
}
return result, nil
}