Skip to content

Commit

Permalink
chore: refactor direct database call from service to model
Browse files Browse the repository at this point in the history
  • Loading branch information
oxiginedev committed Jul 21, 2024
1 parent 13eb8d0 commit 9cf3e34
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
14 changes: 14 additions & 0 deletions internal/models/waitlist_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package models

import (
"errors"
"net/http"
"time"

"github.com/hngprojects/hng_boilerplate_golang_web/pkg/repository/storage/postgresql"
Expand Down Expand Up @@ -33,3 +34,16 @@ func (w *WaitlistUser) CreateWaitlistUser(db *gorm.DB) error {

return err
}

func (w *WaitlistUser) GetWaitlistUserByEmail(db *gorm.DB) (int, error) {
err, nerr := postgresql.SelectOneFromDb(db, &w, "email = ?", w.Email)
if nerr != nil {
return http.StatusBadRequest, nerr
}

if err != nil {
return http.StatusInternalServerError, err
}

return http.StatusOK, nil
}
9 changes: 4 additions & 5 deletions services/waitlist/waitlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"

"github.com/hngprojects/hng_boilerplate_golang_web/internal/models"
"github.com/hngprojects/hng_boilerplate_golang_web/pkg/repository/storage/postgresql"
"github.com/hngprojects/hng_boilerplate_golang_web/utility"
"gorm.io/gorm"
)
Expand All @@ -20,11 +19,11 @@ func SignupWaitlistUserService(db *gorm.DB, req models.CreateWaitlistUserRequest

if req.Email != "" {
req.Email = strings.ToLower(req.Email)
existingUser := models.WaitlistUser{}
_, err := postgresql.SelectOneFromDb(db, &existingUser, "email = ?", req.Email)

existingUser := &models.WaitlistUser{Email: req.Email}
code, err := existingUser.GetWaitlistUserByEmail(db)
if err != nil {
return nil, http.StatusBadRequest, models.ErrWaitlistUserExist
return nil, code, models.ErrWaitlistUserExist
}
}

Expand Down

0 comments on commit 9cf3e34

Please sign in to comment.