Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions domain/comment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package domain

import "time"

type Comment struct {
ID int
ItemID int
UserID string
Text string
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time
}

type CommentRepository interface {
Create(comment *Comment) error
}
7 changes: 4 additions & 3 deletions domain/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package domain
import "errors"

var (
ErrNotFound = errors.New("not found")
ErrInvalidFileType = errors.New("invalid file type: only JPEG and PNG are allowed")
ErrFileTooLarge = errors.New("file too large: max size is 3MB")
ErrNotFound = errors.New("not found")
ErrInvalidFileType = errors.New("invalid file type: only JPEG and PNG are allowed")
ErrFileTooLarge = errors.New("file too large: max size is 3MB")
ErrCommentTextEmpty = errors.New("comment text cannot be empty")
)
44 changes: 44 additions & 0 deletions handler/comment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package handler

import (
"net/http"

"github.com/labstack/echo/v4"
"github.com/traPtitech/booQ-v3/domain"
"github.com/traPtitech/booQ-v3/handler/openapi"
)

func (h *handler) PostComment(ctx echo.Context, itemId openapi.ItemIdInPath) error {

var req openapi.PostComment
if err := ctx.Bind(&req); err != nil {
return ctx.JSON(http.StatusBadRequest, err)
}

// TODO: ミドルウェアからユーザーidを取得するように
userId := "test-user-id"

comment, err := h.cu.CreateComment(itemId, userId, req.Text)
if err != nil {
if err == domain.ErrItemNotFound {

Check failure on line 23 in handler/comment.go

View workflow job for this annotation

GitHub Actions / Server build

undefined: domain.ErrItemNotFound
// Itemがないとき
return ctx.JSON(http.StatusNotFound, "item not found")
} else if err == domain.ErrCommentTextEmpty {
// 投稿されたコメントが空の時
return ctx.JSON(http.StatusBadRequest, "comment text is empty")
}

return ctx.JSON(http.StatusInternalServerError, err)
}

res := openapi.Comment{
Id: &comment.ID,
ItemId: &comment.ItemID,
UserId: &comment.UserID,
Text: comment.Text,
CreatedAt: &comment.CreatedAt,
UpdatedAt: &comment.UpdatedAt,
}

return ctx.JSON(http.StatusCreated, res)
}
9 changes: 3 additions & 6 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (

type handler struct {
iu usecase.ItemUseCase
cu usecase.CommentUsecase
fu usecase.FileUseCase
}

func NewHandler(iu usecase.ItemUseCase, fu usecase.FileUseCase) openapi.ServerInterface {
func NewHandler(iu usecase.ItemUseCase, cu usecase.CommentUsecase, fu usecase.FileUseCase) openapi.ServerInterface {
return &handler{
iu: iu,
fu: fu,
cu: cu,
}
}

Expand Down Expand Up @@ -48,11 +50,6 @@ func (h *handler) PostBorrowEquipmentReturn(ctx echo.Context, itemId openapi.Ite
panic("implement me")
}

func (h *handler) PostComment(ctx echo.Context, itemId openapi.ItemIdInPath) error {
//TODO implement me
panic("implement me")
}

func (h *handler) RemoveLike(ctx echo.Context, itemId openapi.ItemIdInPath) error {
//TODO implement me
panic("implement me")
Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,19 @@ func main() {

// Repository
itemRepo := repository.NewItemRepository(db)
commentRepo := repository.NewCommentRepository(db)
fileRepo := repository.NewFileRepository(db)

// Storage
fileStorage := newFileStorage()

// UseCase
itemUseCase := usecase.NewItemUseCase(itemRepo)
commentUsecase := usecase.NewCommentUsecase(commentRepo, itemRepo)
fileUseCase := usecase.NewFileUseCase(fileRepo, fileStorage)

// Handler
h := handler.NewHandler(itemUseCase, fileUseCase)
h := handler.NewHandler(itemUseCase, commentUsecase, fileUseCase)
openapi.RegisterHandlers(e, h)

e.Logger.Fatal(e.Start(":3001"))
Expand Down
43 changes: 43 additions & 0 deletions repository/comment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package repository

import (
"time"

"github.com/traPtitech/booQ-v3/domain"
"gorm.io/gorm"
)

type comment struct {
GormModel // ID, CreatedAt, UpdatedAt
ItemID int `gorm:"not null"`
UserID string `gorm:"type:varchar(32);not null"`
Text string `gorm:"column:comment;type:text;not null"`
DeletedAt *time.Time
}

type commentRepository struct {
db *gorm.DB
}

func NewCommentRepository(db *gorm.DB) domain.CommentRepository {
return &commentRepository{db: db}
}

func (r *commentRepository) Create(c *domain.Comment) error {
newComment := &comment{
ItemID: c.ItemID,
UserID: c.UserID,
Text: c.Text,
}

if err := r.db.Create(newComment).Error; err != nil {
return err
}

c.ID = newComment.ID
c.CreatedAt = newComment.CreatedAt
c.UpdatedAt = newComment.UpdatedAt
c.DeletedAt = newComment.DeletedAt

return nil
}
1 change: 1 addition & 0 deletions repository/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
var allTables = []interface{}{
item{},
file{},
comment{},
}

type GormModel struct {
Expand Down
53 changes: 53 additions & 0 deletions usecase/comment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package usecase

import (
"github.com/traPtitech/booQ-v3/domain"
)

type CommentUsecase interface {
CreateComment(
itemID int,
userID string,
text string,
) (
*domain.Comment, error,
)
}

type commentUsecase struct {
CommentRepo domain.CommentRepository
ItemRepo domain.ItemRepository
}

func NewCommentUsecase(commentRepo domain.CommentRepository, ItemRepo domain.ItemRepository) CommentUsecase {
return &commentUsecase{
CommentRepo: commentRepo,
ItemRepo: ItemRepo,
}
}

func (u *commentUsecase) CreateComment(itemId int, userId string, text string) (*domain.Comment, error) {

_, err := u.ItemRepo.GetByID(itemId)

if err != nil {
return nil, err
}

if text == "" {
return nil, domain.ErrCommentTextEmpty
}

comment := &domain.Comment{
ItemID: itemId,
UserID: userId,
Text: text,
}

err = u.CommentRepo.Create(comment)
if err != nil {
return nil, err
}

return comment, nil
}
Loading