Skip to content

Commit

Permalink
chore: centralize models
Browse files Browse the repository at this point in the history
  • Loading branch information
ashalfarhan committed Jun 29, 2022
1 parent 5232d65 commit fceea11
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 51 deletions.
5 changes: 2 additions & 3 deletions api/controller/article_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/ashalfarhan/realworld/api/response"
"github.com/ashalfarhan/realworld/conduit"
"github.com/ashalfarhan/realworld/model"
"github.com/ashalfarhan/realworld/persistence/repository"
"github.com/ashalfarhan/realworld/service"
"github.com/ashalfarhan/realworld/utils"
"github.com/ashalfarhan/realworld/utils/jwt"
Expand Down Expand Up @@ -159,10 +158,10 @@ func (c *ArticleController) UnFavoriteArticle(w http.ResponseWriter, r *http.Req
})
}

func getArticleQueryParams(q url.Values) (*repository.FindArticlesArgs, *model.ConduitError) {
func getArticleQueryParams(q url.Values) (*model.FindArticlesArgs, *model.ConduitError) {
var err error
limit, offset := q.Get("limit"), q.Get("offset")
args := &repository.FindArticlesArgs{
args := &model.FindArticlesArgs{
Tag: q.Get("tag"),
Author: q.Get("author"),
Favorited: q.Get("favorited"),
Expand Down
9 changes: 9 additions & 0 deletions model/article_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,12 @@ func (a Articles) MarshalBinary() ([]byte, error) {
func (a *Articles) UnmarshalBinary(data []byte) error {
return json.Unmarshal(data, a)
}

type FindArticlesArgs struct {
Tag string `db:"tag"`
Author string `db:"author_username"`
Username string `db:"username"`
Favorited string `db:"favorited_by"`
Limit int `validate:"min=1,max=25" db:"limit"`
Offset int `validate:"min=0" db:"offset"`
}
19 changes: 12 additions & 7 deletions model/user_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,6 @@ type UserRs struct {
Token string `json:"token,omitempty"`
}

type ProfileRs struct {
Username string `json:"username"`
Bio NullString `json:"bio"`
Image NullString `json:"image"`
Following bool `json:"following"`
}

func (u *User) Serialize(token string) *UserRs {
return &UserRs{
Email: u.Email,
Expand All @@ -46,6 +39,13 @@ func (u *User) Serialize(token string) *UserRs {
}
}

type ProfileRs struct {
Username string `json:"username"`
Bio NullString `json:"bio"`
Image NullString `json:"image"`
Following bool `json:"following"`
}

func (u *User) Profile(following bool) *ProfileRs {
return &ProfileRs{
Username: u.Username,
Expand All @@ -54,3 +54,8 @@ func (u *User) Profile(following bool) *ProfileRs {
Following: following,
}
}

type FindUserArg struct {
Email string
Username string
}
13 changes: 2 additions & 11 deletions persistence/repository/article_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type ArticleRepository interface {
FindOneBySlug(context.Context, string, string) (*model.Article, error)
DeleteBySlug(context.Context, string) error
UpdateOneBySlug(context.Context, *model.UpdateArticleFields, *model.Article) error
Find(context.Context, *FindArticlesArgs) (model.Articles, error)
Find(context.Context, *model.FindArticlesArgs) (model.Articles, error)
}

func (r *ArticleRepoImpl) InsertOne(ctx context.Context, d *model.CreateArticleFields, username string) (*model.Article, error) {
Expand Down Expand Up @@ -127,16 +127,7 @@ func (r *ArticleRepoImpl) FindOneBySlug(ctx context.Context, username, slug stri
return a, nil
}

type FindArticlesArgs struct {
Tag string `db:"tag"`
Author string `db:"author_username"`
Username string `db:"username"`
Favorited string `db:"favorited_by"`
Limit int `validate:"min=1,max=25" db:"limit"`
Offset int `validate:"min=0" db:"offset"`
}

func (r *ArticleRepoImpl) Find(ctx context.Context, p *FindArticlesArgs) (model.Articles, error) {
func (r *ArticleRepoImpl) Find(ctx context.Context, p *model.FindArticlesArgs) (model.Articles, error) {
articles := model.Articles{}
query := `
SELECT
Expand Down
3 changes: 1 addition & 2 deletions persistence/repository/mocks/article_repository_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"

"github.com/ashalfarhan/realworld/model"
"github.com/ashalfarhan/realworld/persistence/repository"
"github.com/stretchr/testify/mock"
)

Expand Down Expand Up @@ -32,7 +31,7 @@ func (m *ArticleRepoMock) UpdateOneBySlug(ctx context.Context, d *model.UpdateAr
return args.Error(0)
}

func (m *ArticleRepoMock) Find(ctx context.Context, a *repository.FindArticlesArgs) (model.Articles, error) {
func (m *ArticleRepoMock) Find(ctx context.Context, a *model.FindArticlesArgs) (model.Articles, error) {
args := m.Called(ctx, a)
return args.Get(0).(model.Articles), args.Error(1)
}
3 changes: 1 addition & 2 deletions persistence/repository/mocks/user_repository_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"

"github.com/ashalfarhan/realworld/model"
"github.com/ashalfarhan/realworld/persistence/repository"
"github.com/stretchr/testify/mock"
)

Expand All @@ -22,7 +21,7 @@ func (m *UserRepoMock) FindOneByUsername(ctx context.Context, s string) (*model.
return arg.Get(0).(*model.User), arg.Error(1)
}

func (m *UserRepoMock) FindOne(ctx context.Context, u *repository.FindOneUserFilter) (*model.User, error) {
func (m *UserRepoMock) FindOne(ctx context.Context, u *model.FindUserArg) (*model.User, error) {
arg := m.Called(ctx, u)
return arg.Get(0).(*model.User), arg.Error(1)
}
Expand Down
9 changes: 2 additions & 7 deletions persistence/repository/user_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type UserRepoImpl struct {
type UserRepository interface {
InsertOne(context.Context, *model.RegisterUserFields) (*model.User, error)
FindOneByUsername(context.Context, string) (*model.User, error)
FindOne(context.Context, *FindOneUserFilter) (*model.User, error)
FindOne(context.Context, *model.FindUserArg) (*model.User, error)
UpdateOne(context.Context, *model.UpdateUserFields, *model.User) error
}

Expand Down Expand Up @@ -60,12 +60,7 @@ func (r *UserRepoImpl) FindOneByUsername(ctx context.Context, username string) (
return u, nil
}

type FindOneUserFilter struct {
Email string
Username string
}

func (r *UserRepoImpl) FindOne(ctx context.Context, d *FindOneUserFilter) (*model.User, error) {
func (r *UserRepoImpl) FindOne(ctx context.Context, d *model.FindUserArg) (*model.User, error) {
u := new(model.User)
query := `
SELECT id, email, username, password, bio, image FROM users
Expand Down
4 changes: 2 additions & 2 deletions service/article_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (s *ArticleService) GetArticleBySlug(ctx context.Context, username, slug st
return ar, nil
}

func (s *ArticleService) GetArticles(ctx context.Context, args *repository.FindArticlesArgs) (model.Articles, *model.ConduitError) {
func (s *ArticleService) GetArticles(ctx context.Context, args *model.FindArticlesArgs) (model.Articles, *model.ConduitError) {
log := logger.GetCtx(ctx)
articles, err := s.articleRepo.Find(ctx, args)
if err != nil {
Expand All @@ -107,7 +107,7 @@ func (s *ArticleService) GetArticles(ctx context.Context, args *repository.FindA
return articles, nil
}

func (s *ArticleService) GetArticlesFeed(ctx context.Context, args *repository.FindArticlesArgs) (model.Articles, *model.ConduitError) {
func (s *ArticleService) GetArticlesFeed(ctx context.Context, args *model.FindArticlesArgs) (model.Articles, *model.ConduitError) {
log := logger.GetCtx(ctx)
articles, err := s.articleRepo.Find(ctx, args)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions service/auth_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/ashalfarhan/realworld/conduit"
"github.com/ashalfarhan/realworld/model"
"github.com/ashalfarhan/realworld/persistence/repository"
"github.com/ashalfarhan/realworld/utils/jwt"
)

Expand All @@ -21,7 +20,7 @@ func NewAuthService(us *UserService) *AuthService {
}

func (s AuthService) Login(ctx context.Context, d *model.LoginUserFields) (*model.UserRs, *model.ConduitError) {
u, sErr := s.userService.GetOne(ctx, &repository.FindOneUserFilter{
u, sErr := s.userService.GetOne(ctx, &model.FindUserArg{
Email: d.Email,
Username: d.Username,
})
Expand Down
1 change: 1 addition & 0 deletions service/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var (
ErrNoUserFound = errors.New("no user found")
ErrEmailExist = errors.New("email already exist")
ErrUsernameExist = errors.New("username already exist")
ErrIdentityExist = errors.New("username or email is in use")
ErrSelfFollow = errors.New("you cannot follow your self")
ErrSelfUnfollow = errors.New("you cannot unfollow your self")
ErrAlreadyFollow = errors.New("you are already follow this user")
Expand Down
4 changes: 2 additions & 2 deletions service/user_following.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func (s *UserService) FollowUser(ctx context.Context, followUsername, username string) (*model.ProfileRs, *model.ConduitError) {
log := logger.GetCtx(ctx)
log.Infof("POST FollowUser followUsername:%q, user:%q", followUsername, username)
following, err := s.GetOne(ctx, &repository.FindOneUserFilter{Username: username})
following, err := s.GetOne(ctx, &model.FindUserArg{Username: username})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -44,7 +44,7 @@ func (s *UserService) FollowUser(ctx context.Context, followUsername, username s
func (s *UserService) UnfollowUser(ctx context.Context, followUsername, username string) (*model.ProfileRs, *model.ConduitError) {
log := logger.GetCtx(ctx)
log.Infof("POST UnfollowUser followUsername:%q, user:%q", followUsername, username)
following, err := s.GetOne(ctx, &repository.FindOneUserFilter{Username: username})
following, err := s.GetOne(ctx, &model.FindUserArg{Username: username})
if err != nil {
return nil, err
}
Expand Down
21 changes: 8 additions & 13 deletions service/user_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (s *UserService) GetOneByUsername(ctx context.Context, username string) (*m
return u, nil
}

func (s *UserService) GetOne(ctx context.Context, d *repository.FindOneUserFilter) (*model.User, *model.ConduitError) {
func (s *UserService) GetOne(ctx context.Context, d *model.FindUserArg) (*model.User, *model.ConduitError) {
log := logger.GetCtx(ctx)
u, err := s.userRepo.FindOne(ctx, d)
if err != nil {
Expand All @@ -52,18 +52,15 @@ func (s *UserService) GetOne(ctx context.Context, d *repository.FindOneUserFilte

func (s *UserService) Insert(ctx context.Context, d *model.RegisterUserFields) (*model.User, *model.ConduitError) {
log := logger.GetCtx(ctx)
exUser, _ := s.GetOne(ctx, &model.FindUserArg{Email: d.Email, Username: d.Username})
if exUser != nil {
return nil, conduit.BuildError(http.StatusBadRequest, ErrIdentityExist)
}
d.Password = s.HashPassword(d.Password)
u, err := s.userRepo.InsertOne(ctx, d)
if err != nil {
switch err.Error() {
case repository.ErrDuplicateEmail:
return nil, conduit.BuildError(http.StatusBadRequest, ErrEmailExist)
case repository.ErrDuplicateUsername:
return nil, conduit.BuildError(http.StatusBadRequest, ErrUsernameExist)
default:
log.Errorf("Cannot insert to user repo reason: %v", err)
return nil, conduit.GeneralError
}
log.Warnln("Cannot insert to user repo reason:", err)
return nil, conduit.GeneralError
}
return u, nil
}
Expand All @@ -74,20 +71,18 @@ func (s *UserService) Update(ctx context.Context, d *model.UpdateUserFields, use
if err != nil {
return nil, err
}

if v := d.Password; v != nil {
hashed := s.HashPassword(*v)
d.Password = &hashed
}

if err := s.userRepo.UpdateOne(ctx, d, u); err != nil {
switch err.Error() {
case repository.ErrDuplicateEmail:
return nil, conduit.BuildError(http.StatusBadRequest, ErrEmailExist)
case repository.ErrDuplicateUsername:
return nil, conduit.BuildError(http.StatusBadRequest, ErrUsernameExist)
default:
log.Errorf("Cannot InsertOne for %+v, Reason: %v", d, err)
log.Warnln("Cannot update user reason:", err)
return nil, conduit.GeneralError
}
}
Expand Down

0 comments on commit fceea11

Please sign in to comment.