Skip to content

Commit

Permalink
Merge pull request #3 from vviveksharma/dal-users
Browse files Browse the repository at this point in the history
Added the DAL function users
  • Loading branch information
vviveksharma authored Jan 29, 2024
2 parents 9ce5cbc + 78f876b commit 7ab7a94
Show file tree
Hide file tree
Showing 16 changed files with 452 additions and 91 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/verify.yaml
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 ./...
21 changes: 21 additions & 0 deletions api/handlers/handlers.go
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
}

52 changes: 52 additions & 0 deletions api/handlers/users.go
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))
}
16 changes: 16 additions & 0 deletions api/routes.go
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)
}
88 changes: 88 additions & 0 deletions api/services/users.go
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
}
Loading

0 comments on commit 7ab7a94

Please sign in to comment.