chore: coded post req for create posts on db
All checks were successful
Backend ci / build (push) Successful in 7m43s
All checks were successful
Backend ci / build (push) Successful in 7m43s
This commit is contained in:
@@ -2,9 +2,19 @@ package handlers
|
||||
|
||||
import (
|
||||
"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/logger"
|
||||
"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/repositories"
|
||||
"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/storage"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type PostHandlers struct {
|
||||
repo repositories.PostRepository
|
||||
}
|
||||
|
||||
func NewPostHandler(repo repositories.PostRepository) *PostHandlers {
|
||||
return &PostHandlers{repo: repo}
|
||||
}
|
||||
|
||||
var log = logger.New(false)
|
||||
|
||||
// GetPosts godoc
|
||||
@@ -14,9 +24,16 @@ var log = logger.New(false)
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} []storage.PostReq
|
||||
// @Router /api/v1/posts [get]
|
||||
func GetPosts(c *gin.Context) {
|
||||
log.Info("GetPosts")
|
||||
// @Router /posts [get]
|
||||
func (h *PostHandlers) GetPosts(c *gin.Context) {
|
||||
var result []storage.PostReq
|
||||
result, err := h.repo.GetAll(c.Request.Context())
|
||||
if err != nil {
|
||||
log.Error("error request: " + err.Error())
|
||||
c.Status(500)
|
||||
}
|
||||
log.Info("200 OK GET /posts")
|
||||
c.JSON(200, result)
|
||||
}
|
||||
|
||||
// GetPost godoc
|
||||
@@ -26,21 +43,40 @@ func GetPosts(c *gin.Context) {
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} storage.PostReq
|
||||
// @Router /api/v1/posts/{id} [get]
|
||||
// @Router /posts/{id} [get]
|
||||
func GetPost(c *gin.Context) {
|
||||
log.Info("GetPost")
|
||||
}
|
||||
|
||||
// CreatePost godoc
|
||||
// @Summary Create post
|
||||
// @Description Create post
|
||||
// @Description Create new post
|
||||
// @Tags posts
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} storage.Post
|
||||
// @Router /api/v1/posts [post]
|
||||
func CreatePost(c *gin.Context) {
|
||||
log.Info("CreatePost")
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param post body storage.PostCreate true "Post data"
|
||||
// @Success 200 {object} storage.PostReq
|
||||
// @Failure 400 {object} gin.H
|
||||
// @Router /posts [post]
|
||||
func (h *PostHandlers) CreatePost(c *gin.Context) {
|
||||
var req storage.PostCreate
|
||||
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(400, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
post := storage.Post{
|
||||
Title: req.Title,
|
||||
Content: req.Content,
|
||||
}
|
||||
|
||||
if err := h.repo.Create(c.Request.Context(), post); err != nil {
|
||||
c.JSON(500, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, post)
|
||||
}
|
||||
|
||||
// UpdatePost godoc
|
||||
@@ -50,7 +86,7 @@ func CreatePost(c *gin.Context) {
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} storage.Post
|
||||
// @Router /api/v1/posts/{id} [put]
|
||||
// @Router /posts/{id} [put]
|
||||
func UpdatePost(c *gin.Context) {
|
||||
log.Info("UpdatePost")
|
||||
}
|
||||
@@ -62,7 +98,7 @@ func UpdatePost(c *gin.Context) {
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} storage.Post
|
||||
// @Router /api/v1/posts/{id} [delete]
|
||||
// @Router /posts/{id} [delete]
|
||||
func DeletePost(c *gin.Context) {
|
||||
log.Info("DeletePost")
|
||||
}
|
||||
|
||||
@@ -1,16 +1,20 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/repositories"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Register(router *gin.Engine) {
|
||||
func Register(router *gin.Engine, db *sql.DB) {
|
||||
handler_posts := NewPostHandler(repositories.NewPostRepository(db))
|
||||
v1 := router.Group("api/v1")
|
||||
posts := v1.Group("posts")
|
||||
{
|
||||
posts.GET("/", GetPosts)
|
||||
posts.GET("/", handler_posts.GetPosts)
|
||||
posts.GET("/:id", GetPost)
|
||||
posts.POST("/", CreatePost)
|
||||
posts.POST("/", handler_posts.CreatePost)
|
||||
posts.PUT("/:id", UpdatePost)
|
||||
posts.DELETE("/:id", DeletePost)
|
||||
}
|
||||
|
||||
@@ -14,13 +14,13 @@ type Logger struct {
|
||||
}
|
||||
|
||||
func New(debug bool) *Logger {
|
||||
logDir := "/var/log/banforge"
|
||||
logDir := "/var/log/backend"
|
||||
if err := os.MkdirAll(logDir, 0750); err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
fileWriter := &lumberjack.Logger{
|
||||
Filename: filepath.Join(logDir, "banforge.log"),
|
||||
Filename: filepath.Join(logDir, "backend.log"),
|
||||
MaxSize: 500,
|
||||
MaxBackups: 3,
|
||||
MaxAge: 28,
|
||||
|
||||
@@ -58,7 +58,19 @@ func (p *postRepository) GetByID(ctx context.Context, id int) (storage.PostReq,
|
||||
}
|
||||
|
||||
func (p *postRepository) Create(ctx context.Context, post storage.Post) error {
|
||||
|
||||
query, err := p.db.Exec(
|
||||
"INSERT INTO posts(title, content) VALUES(?, ?)",
|
||||
post.Title,
|
||||
post.Content,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
id, err := query.LastInsertId()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.logger.Info("Post created:", "id", id)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -12,24 +12,21 @@ var db_path = os.Getenv(
|
||||
"DB_PATH",
|
||||
) + "?_journal_mode=WAL&_busy_timeout=5000&_synchronous=NORMAL&_cache_size=2000&_foreign_keys=ON"
|
||||
|
||||
func CreateTables() error {
|
||||
func CreateTables(db *sql.DB) error {
|
||||
logger := logger.New(false)
|
||||
db, err := sql.Open("sqlite", db_path)
|
||||
if err != nil {
|
||||
logger.Error(err.Error())
|
||||
return err
|
||||
}
|
||||
_, err = db.Exec(Migrations)
|
||||
_, err := db.Exec(Migrations)
|
||||
if err != nil {
|
||||
logger.Error(err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
defer func() {
|
||||
err = db.Close()
|
||||
if err != nil {
|
||||
logger.Error(err.Error())
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
func OpenSession() (*sql.DB, error) {
|
||||
db, err := sql.Open("sqlite", db_path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return db, nil
|
||||
}
|
||||
|
||||
@@ -2,9 +2,9 @@ package storage
|
||||
|
||||
const Migrations = `
|
||||
CREATE TABLE IF NOT EXISTS posts(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT
|
||||
title TEXT NOT NULL
|
||||
content TEXT NOT NULL
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
title TEXT NOT NULL,
|
||||
content TEXT NOT NULL,
|
||||
CREATED_AT DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
`
|
||||
|
||||
@@ -11,3 +11,8 @@ type PostReq struct {
|
||||
Title string `json:"title"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
type PostCreate struct {
|
||||
Title string `json:"title"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user