Skip to content

Commit

Permalink
zopsmart#3 | http layer completed only testcases remainings
Browse files Browse the repository at this point in the history
  • Loading branch information
raybittu committed Feb 11, 2021
1 parent 3d5a39a commit 35d4736
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 13 deletions.
2 changes: 1 addition & 1 deletion configs/.env
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ REDIS_HOST=localhost

DB_HOST=localhost
DB_USER=root
DB_PASSWORD=password
DB_PASSWORD=Bittu@1998
DB_NAME=Hiring
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func main() {

app.GET("/employee/{companyID}", dataHttp.GetAllEmployee)
app.GET("/employee/{companyID}/{employeeID}", dataHttp.GetEmployeeByID)
app.POST("/employee", dataHttp.CreateEmployee)
app.POST("/employee/{companyID}", dataHttp.CreateEmployee)
app.PUT("/employee/{companyID}/{employeeID}", dataHttp.EditEmployee)
app.DELETE("/employee/{companyID}/{employeeID}", dataHttp.DeleteEmployee)

Expand Down
35 changes: 31 additions & 4 deletions panel/http/http.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package http

import (
"strconv"

"github.com/zopsmart/hiring-portal-api/panel/models"

"github.com/vikash/gofr/pkg/gofr"
customeerror "github.com/zopsmart/hiring-portal-api/panel/error"
"github.com/zopsmart/hiring-portal-api/panel/service"
)

Expand All @@ -14,13 +19,28 @@ func New(service service.PanelEmployee) PanelHTTP {
}

func (p PanelHTTP) CreateEmployee(c *gofr.Context) (interface{}, error) {
companyID, err := strconv.Atoi(c.Request.PathParam("companyID"))
if err != nil {
return "Invalid CompanyID", customeerror.ErrInvalidCompanyID
}
var emp models.Employee

return nil, nil
err = c.Request.Bind(&emp)

if err != nil {
return "Wrong json format", customeerror.ErrJSONFormat
}

return p.service.CreateEmployee(c, companyID, &emp)
}

func (p PanelHTTP) GetAllEmployee(c *gofr.Context) (interface{}, error) {
companyID, err := strconv.Atoi(c.Request.PathParam("companyID"))
if err != nil {
return "Invalid CompanyID", customeerror.ErrInvalidCompanyID
}

return nil, nil
return p.service.GetAllEmployee(c, companyID)
}

func (p PanelHTTP) GetEmployeeByID(c *gofr.Context) (interface{}, error) {
Expand All @@ -34,6 +54,13 @@ func (p PanelHTTP) EditEmployee(c *gofr.Context) (interface{}, error) {
}

func (p PanelHTTP) DeleteEmployee(c *gofr.Context) (interface{}, error) {

return nil, nil
companyID, err := strconv.Atoi(c.Request.PathParam("companyID"))
if err != nil {
return "Invalid CompanyID", customeerror.ErrInvalidCompanyID
}
employeeID, err := strconv.Atoi(c.Request.PathParam("employeeID"))
if err != nil {
return "Invalid CompanyID", customeerror.ErrInvalidEmployeeID
}
return p.service.DeleteEmployee(c, companyID, employeeID), nil
}
4 changes: 2 additions & 2 deletions panel/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ func (p PanelEmployeeService) EditEmployee(c *gofr.Context, companyID, employeeI
return *emp, nil
}

func (p PanelEmployeeService) DeleteEmployee(c *gofr.Context, employeeID, companyID int) error {
func (p PanelEmployeeService) DeleteEmployee(c *gofr.Context, companyID, employeeID int) error {
if companyID <= 0 {
return customeerror.ErrInvalidCompanyID
}
if employeeID <= 0 {
return customeerror.ErrInvalidEmployeeID
}
return p.store.DeleteEmployee(c, employeeID, companyID)
return p.store.DeleteEmployee(c, companyID, employeeID)
}
2 changes: 1 addition & 1 deletion panel/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func testDeleteEmployeeService(t *testing.T, c *gofr.Context) {

for i := range testCases {
dataService := New(mockStore{})
err := dataService.DeleteEmployee(c, testCases[i].input.employeeID, testCases[i].input.companyID)
err := dataService.DeleteEmployee(c, testCases[i].input.companyID, testCases[i].input.employeeID)
if err != testCases[i].expectedErr {
t.Errorf("FAILED!! Expected Error %v Got Error %v\n", testCases[i].expectedErr, err)
}
Expand Down
4 changes: 2 additions & 2 deletions panel/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (p PanelStore) CreateEmployee(c *gofr.Context, companyID int, emp *models.E
inputInterface = append(inputInterface, emp.Name, emp.Email, emp.Phone, emp.Designation, emp.YOE, companyID)
rows, err := c.DB.Exec(query, inputInterface...)
if err != nil {
return -1, customeerror.ErrDBServer
return -1, customeerror.ErrRecordNotFound
}
id, _ := rows.LastInsertId()
if id == -1 {
Expand All @@ -86,7 +86,7 @@ func (p PanelStore) EditEmployee(c *gofr.Context, companyID, employeeID int, emp
return int(id), nil
}

func (p PanelStore) DeleteEmployee(c *gofr.Context, employeeID, companyID int) error {
func (p PanelStore) DeleteEmployee(c *gofr.Context, companyID, employeeID int) error {
var inputInterface []interface{}
inputInterface = append(inputInterface, employeeID, companyID)
rows, err := c.DB.Exec("DELETE FROM employee WHERE id=? AND companyId=?", inputInterface...)
Expand Down
4 changes: 2 additions & 2 deletions panel/store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func TestCreateEmployeeMock(t *testing.T) {
{
input: inputData{10, models.Employee{3, "Bittu", "[email protected]", 7250073079, "sde1", 2, 4}},
expectedOutput: -1,
expectedErr: customeerror.ErrDBServer,
expectedErr: customeerror.ErrRecordNotFound,
rowsAffected: 0,
},
}
Expand Down Expand Up @@ -418,7 +418,7 @@ func TestDeleteEmployeeMock(t *testing.T) {
mock.ExpectExec(regexp.QuoteMeta("DELETE FROM employee WHERE id=? AND companyId=?")).
WithArgs(val.input.id, val.input.companyId).
WillReturnResult(sqlmock.NewResult(0, val.rowsAffected))
actualError := dataStore.DeleteEmployee(c, val.input.id, val.input.companyId)
actualError := dataStore.DeleteEmployee(c, val.input.companyId, val.input.id)
if !reflect.DeepEqual(actualError, val.expectedErr) {
t.Errorf("Failed for testDeleteEmployeeMock, testcase: %v, errors don't match \n", i)
t.Errorf("FAILED!! Expected value: %v Actual Output: %v\n", val.expectedErr, actualError)
Expand Down

0 comments on commit 35d4736

Please sign in to comment.