8000 feat(delete_channel):チャンネルの削除を実装 by kasatomorning · Pull Request #37 · comb19/chat_back · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat(delete_channel):チャンネルの削除を実装 #37

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

Merged
merged 1 commit into from
May 20, 2025
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
2 changes: 1 addition & 1 deletion api/cmd/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func SetupRouter() *gin.Engine {
authorized.POST("/channels", channelHandler.HandleInsert)
authorized.GET("/channels/:channelID", channelHandler.HandleGetByID)
authorized.PUT("/channels/:channelID", func(ctx *gin.Context) {})
authorized.DELETE("/channels/:channelID", func(ctx *gin.Context) {})
authorized.DELETE("/channels/:channelID", channelHandler.HandleDelete)
authorized.GET("/channels/:channelID/users", func(ctx *gin.Context) {})
authorized.POST("/channels/:channelID/users", channelHandler.HandleAddUserToChannel)
authorized.GET("/channels/:channelID/messages", channelHandler.HandleGetMessagesInChannel)
Expand Down
1 change: 1 addition & 0 deletions api/domain/repository/channel_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

type ChannelRepository interface {
Insert(name string, description string, private bool, guildID *string) (*model.Channel, error)
Delete(id string) error
GetByID(id string) (*model.Channel, error)
GetAllInGuild(guildID *string) ([]*model.Channel, error)
}
9 changes: 9 additions & 0 deletions api/infrastructure/persistence/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ func (cp *channelPersistence) Insert(name string, description string, private bo
return &channel, result.Error
}

func (cp *channelPersistence) Delete(id string) error {
var channel model.Channel
result := cp.db.Where("id = ?", id).Delete(&channel)
Copy link
Preview
Copilot AI May 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This Delete call does not check RowsAffected, so deleting a non-existent channel returns no error. Consider checking result.RowsAffected and returning a not-found error if zero rows were deleted.

Copilot uses AI. Check for mistakes.

if result.Error != nil {
return result.Error
}
return nil
}

func (cp *channelPersistence) GetByID(id string) (*model.Channel, error) {
var channel model.Channel
result := cp.db.Where("id = ?", id).First(&channel)
Expand Down
18 changes: 18 additions & 0 deletions api/interface/handler/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

type ChannelHandler interface {
HandleInsert(ctx *gin.Context)
HandleDelete(ctx *gin.Context)
HandleGetByID(ctx *gin.Context)
HandleAddUserToChannel(ctx *gin.Context)
HandleGetMessagesInChannel(ctx *gin.Context)
Expand Down Expand Up @@ -62,6 +63,23 @@ func (ch *channelHandler) HandleInsert(ctx *gin.Context) {
})
}

func (ch *channelHandler) HandleDelete(ctx *gin.Context) {
slog.DebugContext(ctx, "HandleDelete")

var uri types.ChannelURI
if err := ctx.BindUri(&uri); err != nil {
slog.ErrorContext(ctx, err.Error())
Copy link
Preview
Copilot AI May 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When URI binding fails, the handler logs the error but does not set a response status, leaving the client hanging. Consider returning a 400 Bad Request (e.g., ctx.AbortWithStatusJSON(http.StatusBadRequest, ...)).

Suggested change
slog.ErrorContext(ctx, err.Error())
slog.ErrorContext(ctx, err.Error())
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid URI parameters"})

Copilot uses AI. Check for mistakes.

return
}
err := ch.channelUseCase.Delete(uri.ID)
if err != nil {
slog.ErrorContext(ctx, err.Error())
ctx.Status(http.StatusInternalServerError)
return
}
ctx.Status(http.StatusNoContent)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ボディーは空でHTTP204だけを返す

}

func (ch *channelHandler) HandleGetByID(ctx *gin.Context) {
slog.DebugContext(ctx, "HandleGetByID")

Expand Down
9 changes: 9 additions & 0 deletions api/usecase/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

type ChannelUsecase interface {
Insert(name string, description string, public bool, ownerID string, guildID *string) (*model.Channel, error)
Delete(id string) error
GetByID(id string) (*model.Channel, error)
AddUserToChannel(id string, userIDs []string) (*model.Channel, error)
GetMessagesOfChannel(id, userID string) (*[]*model.Message, error)
Expand Down Expand Up @@ -43,6 +44,14 @@ func (cu channelUseCase) Insert(name string, description string, public bool, ow
return channel, nil
}

func (cu channelUseCase) Delete(id string) error {
err := cu.channelRepository.Delete(id)
if err != nil {
return err
}
return nil
}

func (cu channelUseCase) GetByID(id string) (*model.Channel, error) {
channel, err := cu.channelRepository.GetByID(id)
if err != nil {
Expand Down
0