From 35d4736badf2995a302e584e6447ab4bc8436ba3 Mon Sep 17 00:00:00 2001 From: raybittu Date: Thu, 11 Feb 2021 18:21:39 +0530 Subject: [PATCH] #3 | http layer completed only testcases remainings --- configs/.env | 2 +- main.go | 2 +- panel/http/http.go | 35 +++++++++++++++++++++++++++++++---- panel/service/service.go | 4 ++-- panel/service/service_test.go | 2 +- panel/store/store.go | 4 ++-- panel/store/store_test.go | 4 ++-- 7 files changed, 40 insertions(+), 13 deletions(-) diff --git a/configs/.env b/configs/.env index 9d6803f7..735fcac9 100644 --- a/configs/.env +++ b/configs/.env @@ -4,5 +4,5 @@ REDIS_HOST=localhost DB_HOST=localhost DB_USER=root -DB_PASSWORD=password +DB_PASSWORD=Bittu@1998 DB_NAME=Hiring \ No newline at end of file diff --git a/main.go b/main.go index a123397f..3cb0fed4 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/panel/http/http.go b/panel/http/http.go index d2fda908..c2c6b9a3 100644 --- a/panel/http/http.go +++ b/panel/http/http.go @@ -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" ) @@ -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) { @@ -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 } diff --git a/panel/service/service.go b/panel/service/service.go index 0b36efd0..02947593 100644 --- a/panel/service/service.go +++ b/panel/service/service.go @@ -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) } diff --git a/panel/service/service_test.go b/panel/service/service_test.go index 4c1d9845..00a4f8be 100644 --- a/panel/service/service_test.go +++ b/panel/service/service_test.go @@ -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) } diff --git a/panel/store/store.go b/panel/store/store.go index 249f1e0e..2b73b673 100644 --- a/panel/store/store.go +++ b/panel/store/store.go @@ -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 { @@ -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...) diff --git a/panel/store/store_test.go b/panel/store/store_test.go index 47849f05..d8b2c08b 100644 --- a/panel/store/store_test.go +++ b/panel/store/store_test.go @@ -250,7 +250,7 @@ func TestCreateEmployeeMock(t *testing.T) { { input: inputData{10, models.Employee{3, "Bittu", "bittu@gmail.com", 7250073079, "sde1", 2, 4}}, expectedOutput: -1, - expectedErr: customeerror.ErrDBServer, + expectedErr: customeerror.ErrRecordNotFound, rowsAffected: 0, }, } @@ -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)