-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
9ce5cbc
commit 78f876b
Showing
16 changed files
with
452 additions
and
91 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
name: Verify Pipeline | ||
|
||
on: | ||
# Trigger the verification workflow on push or pull request, for the main branch. | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
verify-pipeline: | ||
name: Verify Pipeline Job | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout Repository | ||
uses: actions/checkout@v4 | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v4 | ||
with: | ||
go-version: 1.21.3 | ||
|
||
- name: Verify dependencies | ||
run: go mod verify | ||
|
||
- name: Download dependencies | ||
run: go get ./... | ||
|
||
- name: Run go vet | ||
run: go vet ./... | ||
|
||
- name: Run Unit Tests | ||
run: go test -race -vet=off ./... |
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,21 @@ | ||
package handlers | ||
|
||
import ( | ||
"log" | ||
"voting-system/api/services" | ||
) | ||
|
||
type Handler struct { | ||
Logger *log.Logger | ||
UserService services.IUserService | ||
} | ||
|
||
func NewHandler(logger *log.Logger) *Handler { | ||
return &Handler{Logger: logger} | ||
} | ||
|
||
func (h *Handler) UserServiceInstance(us services.IUserService) *Handler { | ||
h.UserService = us | ||
return h | ||
} | ||
|
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,52 @@ | ||
package handlers | ||
|
||
import ( | ||
"errors" | ||
"voting-system/models" | ||
|
||
"github.com/gofiber/fiber/v2" | ||
) | ||
|
||
func (h *Handler) UserRegister(c *fiber.Ctx) error { | ||
var requestBody *models.UserResgiterRequestBody | ||
err := c.BodyParser(&requestBody) | ||
if err != nil { | ||
return c.Status(fiber.StatusBadGateway).JSON(errors.New("error while parsing the request Body")) | ||
} | ||
if requestBody.Email == "" || requestBody.Password == "" || requestBody.UserName == "" || requestBody.FName == "" || requestBody.LName == "" { | ||
return c.Status(fiber.ErrBadRequest.Code).JSON(models.Failed(&fiber.Error{ | ||
Code: fiber.StatusBadRequest, | ||
Message: errors.New("email, password, Username, lastname and firstName cannot be empty").Error(), | ||
})) | ||
} | ||
response, err := h.UserService.UserRegister(requestBody) | ||
if err != nil { | ||
return c.Status(fiber.StatusInternalServerError).JSON(models.Failed(&fiber.Error{ | ||
Code: fiber.StatusInternalServerError, | ||
Message: err.Error(), | ||
})) | ||
} | ||
return c.JSON(models.Success(response)) | ||
} | ||
|
||
func (h *Handler) UserLogin(c *fiber.Ctx) error { | ||
var requestBody *models.UserLoginRequestBody | ||
err := c.BodyParser(&requestBody) | ||
if err != nil { | ||
return c.Status(fiber.StatusBadGateway).JSON(errors.New("error while parsing the request Body")) | ||
} | ||
if requestBody.VoterId == "" { | ||
return c.Status(fiber.ErrBadRequest.Code).JSON(models.Failed(&fiber.Error{ | ||
Code: fiber.StatusBadRequest, | ||
Message: errors.New("voter_Id cannot be empty").Error(), | ||
})) | ||
} | ||
response, err := h.UserService.UserLogin(requestBody) | ||
if err != nil { | ||
return c.Status(fiber.StatusInternalServerError).JSON(models.Failed(&fiber.Error{ | ||
Code: fiber.StatusInternalServerError, | ||
Message: err.Error(), | ||
})) | ||
} | ||
return c.JSON(models.Success(response)) | ||
} |
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,16 @@ | ||
package routes | ||
|
||
import ( | ||
"voting-system/api/handlers" | ||
|
||
"github.com/gofiber/fiber/v2" | ||
) | ||
|
||
func Routes(app *fiber.App, h *handlers.Handler) { | ||
app.Get("/health-check", func(c *fiber.Ctx) error { | ||
return c.SendString("Health Check is working fine") | ||
}) | ||
|
||
app.Post("/users/register", h.UserRegister) | ||
app.Post("/users/login", h.UserLogin) | ||
} |
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,88 @@ | ||
package services | ||
|
||
import ( | ||
"errors" | ||
"voting-system/models" | ||
"voting-system/repos" | ||
|
||
"github.com/google/uuid" | ||
) | ||
|
||
type IUserService interface { | ||
UserRegister(*models.UserResgiterRequestBody) (*models.UserRegisterResponseBody, error) | ||
UserLogin(*models.UserLoginRequestBody) (*models.UserLoginResponseBody, error) | ||
} | ||
|
||
func (us *UserService) setupUserInstance() error { | ||
var err error | ||
us.UserRepo, err = repos.NewUserRequest() | ||
if err != nil { | ||
return errors.New("error in the repo intilization") | ||
} | ||
return nil | ||
} | ||
|
||
type UserService struct { | ||
UserRepo repos.User | ||
} | ||
|
||
func NewUserService() *UserService { | ||
return &UserService{} | ||
} | ||
|
||
func (us *UserService) UserRegister(requestBody *models.UserResgiterRequestBody) (*models.UserRegisterResponseBody, error) { | ||
err := us.setupUserInstance() | ||
if err != nil { | ||
return nil, errors.New("error while intializig the user repos") | ||
} | ||
userDetails, err := us.UserRepo.Find(&models.DbUser{ | ||
Email: requestBody.Email, | ||
}) | ||
if err != nil { | ||
return nil, errors.New("error while finding the user") | ||
} | ||
if userDetails.Email == requestBody.Email { | ||
return nil, errors.New("user already exist please login") | ||
} | ||
voterId := uuid.New() | ||
err = us.UserRepo.Create(&models.DbUser{ | ||
Username: requestBody.UserName, | ||
SecretKey: requestBody.Password, | ||
Fname: requestBody.FName, | ||
Lname: requestBody.LName, | ||
Email: requestBody.Email, | ||
VoterID: voterId, | ||
}) | ||
if err != nil { | ||
return nil, errors.New("unable to create the user from the repos layer") | ||
} | ||
return &models.UserRegisterResponseBody{ | ||
Response: voterId.String(), | ||
}, nil | ||
} | ||
|
||
func (us *UserService) UserLogin(requestBody *models.UserLoginRequestBody) (*models.UserLoginResponseBody, error) { | ||
err := us.setupUserInstance() | ||
if err != nil { | ||
return nil, errors.New("error while intializig the user repos") | ||
} | ||
parsedUUID, err := uuid.Parse(requestBody.VoterId) | ||
if err != nil { | ||
return nil, errors.New("error while parsing the requestBody uuid to UUID format") | ||
} | ||
|
||
userDetails, err := us.UserRepo.Find(&models.DbUser{ | ||
VoterID: parsedUUID, | ||
}) | ||
|
||
if err != nil { | ||
return nil, errors.New("error while finding the user") | ||
} | ||
|
||
if !userDetails.IsValidate { | ||
return nil, errors.New("please validate the token sent on your email") | ||
} | ||
return &models.UserLoginResponseBody{ | ||
Response: "User Logged in Successfully", | ||
}, nil | ||
} |
Oops, something went wrong.