-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
315 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/samdyra/go-geo/internal/models" | ||
"github.com/samdyra/go-geo/internal/services" | ||
"github.com/samdyra/go-geo/internal/utils/errors" | ||
) | ||
|
||
type ArticleHandler struct { | ||
articleService *services.ArticleService | ||
} | ||
|
||
func NewArticleHandler(articleService *services.ArticleService) *ArticleHandler { | ||
return &ArticleHandler{articleService: articleService} | ||
} | ||
|
||
func (h *ArticleHandler) GetArticles(c *gin.Context) { | ||
articles, err := h.articleService.GetArticles() | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, errors.NewAPIError(err)) | ||
return | ||
} | ||
c.JSON(http.StatusOK, articles) | ||
} | ||
|
||
func (h *ArticleHandler) GetArticle(c *gin.Context) { | ||
id, err := strconv.ParseInt(c.Param("id"), 10, 64) | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, errors.NewAPIError(errors.ErrInvalidInput)) | ||
return | ||
} | ||
|
||
article, err := h.articleService.GetArticleByID(id) | ||
if err != nil { | ||
c.JSON(http.StatusNotFound, errors.NewAPIError(err)) | ||
return | ||
} | ||
c.JSON(http.StatusOK, article) | ||
} | ||
|
||
func (h *ArticleHandler) CreateArticle(c *gin.Context) { | ||
var input models.CreateArticleInput | ||
if err := c.ShouldBindJSON(&input); err != nil { | ||
c.JSON(http.StatusBadRequest, errors.NewAPIError(errors.ErrInvalidInput)) | ||
return | ||
} | ||
|
||
if err := input.Validate(); err != nil { | ||
c.JSON(http.StatusBadRequest, errors.NewAPIError(err)) | ||
return | ||
} | ||
|
||
userID, exists := c.Get("user_id") | ||
if !exists { | ||
c.JSON(http.StatusUnauthorized, errors.NewAPIError(errors.ErrUnauthorized)) | ||
return | ||
} | ||
|
||
userIDInt, ok := userID.(int64) | ||
if !ok { | ||
c.JSON(http.StatusInternalServerError, errors.NewAPIError(errors.ErrInternalServer)) | ||
return | ||
} | ||
|
||
article, err := h.articleService.CreateArticle(input, userIDInt) | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, errors.NewAPIError(err)) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusCreated, article) | ||
} | ||
|
||
func (h *ArticleHandler) UpdateArticle(c *gin.Context) { | ||
id, err := strconv.ParseInt(c.Param("id"), 10, 64) | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, errors.NewAPIError(errors.ErrInvalidInput)) | ||
return | ||
} | ||
|
||
var input models.UpdateArticleInput | ||
if err := c.ShouldBindJSON(&input); err != nil { | ||
c.JSON(http.StatusBadRequest, errors.NewAPIError(errors.ErrInvalidInput)) | ||
return | ||
} | ||
|
||
if err := input.Validate(); err != nil { | ||
c.JSON(http.StatusBadRequest, errors.NewAPIError(err)) | ||
return | ||
} | ||
|
||
userID, _ := c.Get("user_id") | ||
article, err := h.articleService.UpdateArticle(id, input, userID.(int64)) | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, errors.NewAPIError(err)) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, article) | ||
} | ||
|
||
func (h *ArticleHandler) DeleteArticle(c *gin.Context) { | ||
id, err := strconv.ParseInt(c.Param("id"), 10, 64) | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, errors.NewAPIError(errors.ErrInvalidInput)) | ||
return | ||
} | ||
|
||
userID, _ := c.Get("user_id") | ||
err = h.articleService.DeleteArticle(id, userID.(int64)) | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, errors.NewAPIError(err)) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, gin.H{"message": "Article deleted successfully"}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package models | ||
|
||
import ( | ||
"time" | ||
|
||
validation "github.com/go-ozzo/ozzo-validation/v4" | ||
"github.com/go-ozzo/ozzo-validation/v4/is" | ||
) | ||
|
||
type Article struct { | ||
ID int64 `db:"id" json:"id"` | ||
Title string `db:"title" json:"title"` | ||
Author string `db:"author" json:"author"` | ||
Content string `db:"content" json:"content"` | ||
ImageURL *string `db:"image_url" json:"image_url,omitempty"` | ||
CreatedBy int64 `db:"created_by" json:"created_by"` | ||
CreatedAt time.Time `db:"created_at" json:"created_at"` | ||
} | ||
|
||
type CreateArticleInput struct { | ||
Title string `json:"title"` | ||
Content string `json:"content"` | ||
ImageURL *string `json:"image_url,omitempty"` | ||
} | ||
|
||
type UpdateArticleInput struct { | ||
Title *string `json:"title,omitempty"` | ||
Content *string `json:"content,omitempty"` | ||
ImageURL *string `json:"image_url,omitempty"` | ||
} | ||
|
||
func (i CreateArticleInput) Validate() error { | ||
return validation.ValidateStruct(&i, | ||
validation.Field(&i.Title, validation.Required, validation.Length(1, 255)), | ||
validation.Field(&i.Content, validation.Required), | ||
validation.Field(&i.ImageURL, validation.NilOrNotEmpty, is.URL), | ||
) | ||
} | ||
|
||
func (i UpdateArticleInput) Validate() error { | ||
return validation.ValidateStruct(&i, | ||
validation.Field(&i.Title, validation.NilOrNotEmpty, validation.Length(1, 255)), | ||
validation.Field(&i.Content, validation.NilOrNotEmpty), | ||
validation.Field(&i.ImageURL, validation.NilOrNotEmpty, is.URL), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package services | ||
|
||
import ( | ||
"database/sql" | ||
"log" | ||
"time" | ||
|
||
"github.com/jmoiron/sqlx" | ||
"github.com/samdyra/go-geo/internal/models" | ||
"github.com/samdyra/go-geo/internal/utils/errors" | ||
) | ||
|
||
type ArticleService struct { | ||
db *sqlx.DB | ||
} | ||
|
||
func NewArticleService(db *sqlx.DB) *ArticleService { | ||
return &ArticleService{db: db} | ||
} | ||
|
||
func (s *ArticleService) GetArticles() ([]models.Article, error) { | ||
var articles []models.Article | ||
err := s.db.Select(&articles, "SELECT * FROM articles ORDER BY created_at DESC") | ||
if err != nil { | ||
return nil, errors.ErrInternalServer | ||
} | ||
return articles, nil | ||
} | ||
|
||
func (s *ArticleService) GetArticleByID(id int64) (*models.Article, error) { | ||
var article models.Article | ||
err := s.db.Get(&article, "SELECT * FROM articles WHERE id = $1", id) | ||
if err == sql.ErrNoRows { | ||
return nil, errors.ErrNotFound | ||
} | ||
if err != nil { | ||
return nil, errors.ErrInternalServer | ||
} | ||
return &article, nil | ||
} | ||
|
||
func (s *ArticleService) CreateArticle(input models.CreateArticleInput, userID int64) (*models.Article, error) { | ||
var username string | ||
err := s.db.Get(&username, "SELECT username FROM users WHERE id = $1", userID) | ||
if err != nil { | ||
log.Printf("Error fetching username: %v", err) | ||
return nil, errors.ErrInternalServer | ||
} | ||
|
||
article := &models.Article{ | ||
Title: input.Title, | ||
Content: input.Content, | ||
ImageURL: input.ImageURL, | ||
Author: username, | ||
CreatedBy: userID, | ||
CreatedAt: time.Now(), | ||
} | ||
|
||
query := `INSERT INTO articles (title, content, image_url, author, created_by, created_at) | ||
VALUES ($1, $2, $3, $4, $5, $6) RETURNING id` | ||
err = s.db.QueryRow(query, article.Title, article.Content, article.ImageURL, article.Author, article.CreatedBy, article.CreatedAt). | ||
Scan(&article.ID) | ||
if err != nil { | ||
log.Printf("Error creating article: %v", err) | ||
return nil, errors.ErrInternalServer | ||
} | ||
|
||
return article, nil | ||
} | ||
|
||
func (s *ArticleService) UpdateArticle(id int64, input models.UpdateArticleInput, userID int64) (*models.Article, error) { | ||
article, err := s.GetArticleByID(id) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if article.CreatedBy != userID { | ||
return nil, errors.ErrUnauthorized | ||
} | ||
|
||
if input.Title != nil { | ||
article.Title = *input.Title | ||
} | ||
if input.Content != nil { | ||
article.Content = *input.Content | ||
} | ||
if input.ImageURL != nil { | ||
article.ImageURL = input.ImageURL | ||
} | ||
|
||
query := `UPDATE articles SET title = $1, content = $2, image_url = $3 WHERE id = $4` | ||
_, err = s.db.Exec(query, article.Title, article.Content, article.ImageURL, id) | ||
if err != nil { | ||
return nil, errors.ErrInternalServer | ||
} | ||
|
||
return article, nil | ||
} | ||
|
||
func (s *ArticleService) DeleteArticle(id int64, userID int64) error { | ||
article, err := s.GetArticleByID(id) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if article.CreatedBy != userID { | ||
return errors.ErrUnauthorized | ||
} | ||
|
||
_, err = s.db.Exec("DELETE FROM articles WHERE id = $1", id) | ||
if err != nil { | ||
return errors.ErrInternalServer | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE IF EXISTS articles; |
Oops, something went wrong.