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
Hiroshi Kurabayashi
committed
Jan 21, 2020
1 parent
94c08c1
commit d8629eb
Showing
4 changed files
with
59 additions
and
16 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
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,42 @@ | ||
package controllers | ||
|
||
import ( | ||
"github.com/jinzhu/gorm" | ||
|
||
"api/domain/model" | ||
) | ||
|
||
type UserController interface { | ||
CreateUser(data *model.User) bool | ||
GetAllUsers() (model.Users, error) | ||
GetUser(id int) (model.User, error) | ||
} | ||
|
||
type userController struct { | ||
db *gorm.DB | ||
} | ||
|
||
func NewUserController(db *gorm.DB) UserController { | ||
return &userController{db: db} | ||
} | ||
|
||
func (uc *userController) CreateUser(data *model.User) bool { | ||
uc.db.NewRecord(data) | ||
uc.db.Create(&data) | ||
return uc.db.NewRecord(data) | ||
} | ||
|
||
func (uc *userController) GetAllUsers() (model.Users, error) { | ||
users := model.Users{} | ||
err := uc.db.Find(&users).Error | ||
if err != nil { | ||
return nil, err | ||
} | ||
return users, nil | ||
} | ||
|
||
func (uc *userController) GetUser(id int) (model.User, error) { | ||
user := model.User{} | ||
err := uc.db.Where("Id = ?", id).First(&user).Error | ||
return user, err | ||
} |
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