-
Notifications
You must be signed in to change notification settings - Fork 0
Room API実装 #20
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
Room API実装 #20
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a12ff01
Update OpenAPI schema from dotto-typespec
kantacky 27e633b
roomのDB
hikaru-0602 5e76ac6
roomのドメイン
hikaru-0602 466b162
Roomのリポジトリ
hikaru-0602 0825457
RoomService
hikaru-0602 d8d41e8
RoomのCRUD
hikaru-0602 84e13a5
Convert
hikaru-0602 f305d12
Handler実装
hikaru-0602 2eafc5b
Mainに追加
hikaru-0602 fbaabc7
task geneate
hikaru-0602 0519fa2
パラメータ名修正
hikaru-0602 0e5b4e2
スタブ実装
hikaru-0602 80961e1
Merge branch 'update-openapi-schema' into feature/room-api
hikaru-0602 409f0f5
RoomのFilter追加
hikaru-0602 9e9462d
Merge branch 'main' into feature/room-api
hikaru-0602 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,32 @@ | ||
| package database | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| "github.com/fun-dotto/academic-api/internal/domain" | ||
| ) | ||
|
|
||
| type Room struct { | ||
| ID string `gorm:"type:uuid;primaryKey"` | ||
| Name string `gorm:"not null"` | ||
| Floor string `gorm:"not null"` | ||
|
|
||
| CreatedAt time.Time | ||
| UpdatedAt time.Time | ||
| } | ||
|
|
||
| func RoomToDomain(m Room) domain.Room { | ||
| return domain.Room{ | ||
| ID: m.ID, | ||
| Name: m.Name, | ||
| Floor: domain.Floor(m.Floor), | ||
| } | ||
| } | ||
|
|
||
| func RoomFromDomain(d domain.Room) Room { | ||
| return Room{ | ||
| ID: d.ID, | ||
| Name: d.Name, | ||
| Floor: string(d.Floor), | ||
| } | ||
| } |
This file contains hidden or 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,24 @@ | ||
| package domain | ||
|
|
||
| type Floor string | ||
|
|
||
| const ( | ||
| FloorFloor1 Floor = "Floor1" | ||
| FloorFloor2 Floor = "Floor2" | ||
| FloorFloor3 Floor = "Floor3" | ||
| FloorFloor4 Floor = "Floor4" | ||
| FloorFloor5 Floor = "Floor5" | ||
| FloorFloor6 Floor = "Floor6" | ||
| FloorFloor7 Floor = "Floor7" | ||
| ) | ||
|
|
||
| type Room struct { | ||
| ID string | ||
| Name string | ||
| Floor Floor | ||
| } | ||
|
|
||
| type RoomListFilter struct { | ||
| IDs []string | ||
| Floors []Floor | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,91 @@ | ||
| package repository | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
|
|
||
| "github.com/fun-dotto/academic-api/internal/database" | ||
| "github.com/fun-dotto/academic-api/internal/domain" | ||
| "gorm.io/gorm" | ||
| ) | ||
|
|
||
| type RoomRepository struct { | ||
| db *gorm.DB | ||
| } | ||
|
|
||
| func NewRoomRepository(db *gorm.DB) *RoomRepository { | ||
| return &RoomRepository{db: db} | ||
| } | ||
|
|
||
| func (r *RoomRepository) List(ctx context.Context, filter domain.RoomListFilter) ([]domain.Room, error) { | ||
| var dbRooms []database.Room | ||
| query := r.db.WithContext(ctx) | ||
| if len(filter.IDs) > 0 { | ||
| query = query.Where("id IN ?", filter.IDs) | ||
| } | ||
| if len(filter.Floors) > 0 { | ||
| floors := make([]string, len(filter.Floors)) | ||
| for i, f := range filter.Floors { | ||
| floors[i] = string(f) | ||
| } | ||
| query = query.Where("floor IN ?", floors) | ||
| } | ||
| if err := query.Find(&dbRooms).Error; err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| domainRooms := make([]domain.Room, len(dbRooms)) | ||
| for i, dbRoom := range dbRooms { | ||
| domainRooms[i] = database.RoomToDomain(dbRoom) | ||
| } | ||
|
|
||
| return domainRooms, nil | ||
| } | ||
|
|
||
| func (r *RoomRepository) GetByID(ctx context.Context, id string) (domain.Room, error) { | ||
| var dbRoom database.Room | ||
| if err := r.db.WithContext(ctx).First(&dbRoom, "id = ?", id).Error; err != nil { | ||
| return domain.Room{}, err | ||
| } | ||
| return database.RoomToDomain(dbRoom), nil | ||
| } | ||
|
|
||
| func (r *RoomRepository) Create(ctx context.Context, room domain.Room) (domain.Room, error) { | ||
| dbRoom := database.RoomFromDomain(room) | ||
| if err := r.db.WithContext(ctx).Create(&dbRoom).Error; err != nil { | ||
| return domain.Room{}, err | ||
| } | ||
| return database.RoomToDomain(dbRoom), nil | ||
| } | ||
|
|
||
| func (r *RoomRepository) Update(ctx context.Context, room domain.Room) (domain.Room, error) { | ||
| dbRoom := database.RoomFromDomain(room) | ||
| if err := r.db.WithContext(ctx).Model(&database.Room{}).Where("id = ?", room.ID).Updates(map[string]interface{}{ | ||
| "name": dbRoom.Name, | ||
| "floor": dbRoom.Floor, | ||
| }).Error; err != nil { | ||
| return domain.Room{}, err | ||
| } | ||
| return r.GetByID(ctx, room.ID) | ||
| } | ||
|
|
||
| func (r *RoomRepository) Delete(ctx context.Context, id string) error { | ||
| return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error { | ||
| var room database.Room | ||
| if err := tx.Where("id = ?", id).First(&room).Error; err != nil { | ||
| if errors.Is(err, gorm.ErrRecordNotFound) { | ||
| return err | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| result := tx.Where("id = ?", id).Delete(&database.Room{}) | ||
| if result.Error != nil { | ||
| return result.Error | ||
| } | ||
| if result.RowsAffected == 0 { | ||
| return gorm.ErrRecordNotFound | ||
| } | ||
| return nil | ||
| }) | ||
hikaru-0602 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
This file contains hidden or 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,43 @@ | ||
| package service | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/fun-dotto/academic-api/internal/domain" | ||
| ) | ||
|
|
||
| type roomRepository interface { | ||
| List(ctx context.Context, filter domain.RoomListFilter) ([]domain.Room, error) | ||
| GetByID(ctx context.Context, id string) (domain.Room, error) | ||
| Create(ctx context.Context, room domain.Room) (domain.Room, error) | ||
| Update(ctx context.Context, room domain.Room) (domain.Room, error) | ||
| Delete(ctx context.Context, id string) error | ||
| } | ||
|
|
||
| type RoomService struct { | ||
| repo roomRepository | ||
| } | ||
|
|
||
| func NewRoomService(repo roomRepository) *RoomService { | ||
| return &RoomService{repo: repo} | ||
| } | ||
|
|
||
| func (s *RoomService) List(ctx context.Context, filter domain.RoomListFilter) ([]domain.Room, error) { | ||
| return s.repo.List(ctx, filter) | ||
| } | ||
hikaru-0602 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| func (s *RoomService) GetByID(ctx context.Context, id string) (domain.Room, error) { | ||
| return s.repo.GetByID(ctx, id) | ||
| } | ||
|
|
||
| func (s *RoomService) Create(ctx context.Context, room domain.Room) (domain.Room, error) { | ||
| return s.repo.Create(ctx, room) | ||
| } | ||
|
|
||
| func (s *RoomService) Update(ctx context.Context, room domain.Room) (domain.Room, error) { | ||
| return s.repo.Update(ctx, room) | ||
| } | ||
|
|
||
| func (s *RoomService) Delete(ctx context.Context, id string) error { | ||
| return s.repo.Delete(ctx, id) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.