Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

チャンネルの取得 API のアップデート #66

Merged
merged 13 commits into from
Jun 27, 2024
Merged
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
20 changes: 15 additions & 5 deletions backend/timeline/internal/controllers/get_channel_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@ import (
"errors"
"myapp/internal/entities"
"myapp/internal/repositories"
"myapp/internal/usecases"
"net/http"
"strconv"
"time"

"github.com/gin-gonic/gin"
)

func GetChannels(ctx *gin.Context) {
repository := repositories.NewChannelsRepository(DB(ctx))
channels, err := repository.Get()
serverId, err := strconv.Atoi(ctx.Param("serverId"))
if err != nil {
ctx.JSON(http.StatusBadRequest, err.Error())
}

serverRepository := repositories.NewGetServerRepository(DB(ctx))
getChannelsRepository := repositories.NewGetChannelsRepository(DB(ctx))
usecase := usecases.NewGetChannelsUsecase(serverRepository, getChannelsRepository)
channels, err := usecase.Execute(serverId)
if err != nil {
handleError(ctx, 500, err)
} else if channels != nil {
Expand Down Expand Up @@ -41,8 +51,8 @@ type ChannelsResponseJson struct {

type ChannelJson struct {
Id int `json:"id"`
ServerId int `json:"serverId"`
ServerId int `json:"server_id"`
Name string `json:"name"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package interfaces

import "myapp/internal/entities"

type GetChannelsRepository interface {
Get(serverId int) ([]entities.Channel, error)
}
2 changes: 1 addition & 1 deletion backend/timeline/internal/middleware/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func SetupRoutes(app *gin.Engine) {
authorized.Use(Auth())
{
authorized.GET("/servers", controllers.GetServers)
authorized.GET("/channels", controllers.GetChannels)
authorized.GET("/servers/:serverId/channels", controllers.GetChannels)
authorized.POST("/servers/:id/channels", controllers.CreateChannels)
authorized.GET("/channels/:id/posts", controllers.GetPosts)
authorized.POST("/channels/:id/posts", controllers.CreatePost)
Expand Down
20 changes: 13 additions & 7 deletions backend/timeline/internal/repositories/get_channels_repository.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package repositories

import (
"fmt"
"gorm.io/gorm"
"time"
"myapp/internal/entities"
"time"

"gorm.io/gorm"
)

type GetChannelsRepository struct {
Expand All @@ -21,16 +21,22 @@ type Channel struct {
DeletedAt time.Time
}

func NewChannelsRepository(conn *gorm.DB) *GetChannelsRepository {
func NewGetChannelsRepository(conn *gorm.DB) *GetChannelsRepository {
return &GetChannelsRepository{
Conn: conn,
}
}

func (r *GetChannelsRepository) Get() ([]entities.Channel, error) {
func (r *GetChannelsRepository) Get(serverId int) ([]entities.Channel, error) {

obj := []Channel{}
r.Conn.Find(&obj)
fmt.Printf("result: %+v\n", obj)
if err := r.Conn.Table("channels").
Select("id, server_id, name, created_at, updated_at").
Where("server_id = ?", serverId).
Where("deleted_at IS NULL").
Find(&obj).Error; err != nil {
return nil, err
}
return convertChannelsRepositoryModelToEntity(obj), nil
}

Expand Down
28 changes: 28 additions & 0 deletions backend/timeline/internal/usecases/get_channels_usecase.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package usecases

import (
"myapp/internal/entities"
"myapp/internal/interfaces"
)

type GetChannelsUsecase struct {
serverRepository interfaces.ServerRepository
getChannelsRepository interfaces.GetChannelsRepository
}

func NewGetChannelsUsecase(sr interfaces.ServerRepository, gcr interfaces.GetChannelsRepository) *GetChannelsUsecase {
return &GetChannelsUsecase{
serverRepository: sr,
getChannelsRepository: gcr,
}
}

func (uc *GetChannelsUsecase) Execute(serverId int) ([]entities.Channel, error) {
// 存在しているサーバーか確認する
if _, err := uc.serverRepository.Get(serverId); err != nil {
return nil, err
}

result, err := uc.getChannelsRepository.Get(serverId)
return result, err
}
Loading