-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from Cyberguru1/main
[FEAT] API Endpoint For Organisation Creation by users
- Loading branch information
Showing
20 changed files
with
1,157 additions
and
20 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
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
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
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 |
---|---|---|
|
@@ -18,6 +18,7 @@ func SeedDatabase(db *gorm.DB) { | |
ID: Userid1, | ||
Name: "John Doe", | ||
Email: "[email protected]", | ||
Password: utility.RandomString(20), | ||
Profile: models.Profile{ | ||
ID: utility.GenerateUUID(), | ||
FirstName: "John", | ||
|
@@ -35,6 +36,7 @@ func SeedDatabase(db *gorm.DB) { | |
user2 := models.User{ | ||
ID: Userid2, | ||
Name: "Jane Doe", | ||
Password: utility.RandomString(20), | ||
Email: "[email protected]", | ||
Profile: models.Profile{ | ||
ID: utility.GenerateUUID(), | ||
|
@@ -50,9 +52,9 @@ func SeedDatabase(db *gorm.DB) { | |
} | ||
|
||
organisations := []models.Organisation{ | ||
{ID: utility.GenerateUUID(), Name: "Org1", Description: "Description1"}, | ||
{ID: utility.GenerateUUID(), Name: "Org2", Description: "Description2"}, | ||
{ID: utility.GenerateUUID(), Name: "Org3", Description: "Description3"}, | ||
{ID: utility.GenerateUUID(), Name: "Org1", Email: fmt.Sprintf(utility.RandomString(4)+"@email.com"),Description: "Description1", OwnerID: Userid1}, | ||
{ID: utility.GenerateUUID(), Name: "Org2", Email: fmt.Sprintf(utility.RandomString(4)+"@email.com"),Description: "Description2", OwnerID: Userid1}, | ||
{ID: utility.GenerateUUID(), Name: "Org3", Email: fmt.Sprintf(utility.RandomString(4)+"@email.com"),Description: "Description3", OwnerID: Userid2}, | ||
} | ||
|
||
var existingUser models.User | ||
|
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
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,73 @@ | ||
package organisation | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/go-playground/validator/v10" | ||
"github.com/golang-jwt/jwt" | ||
|
||
"github.com/hngprojects/hng_boilerplate_golang_web/external/request" | ||
"github.com/hngprojects/hng_boilerplate_golang_web/internal/models" | ||
"github.com/hngprojects/hng_boilerplate_golang_web/pkg/repository/storage" | ||
service "github.com/hngprojects/hng_boilerplate_golang_web/services/organisation" | ||
"github.com/hngprojects/hng_boilerplate_golang_web/utility" | ||
) | ||
|
||
type Controller struct { | ||
Db *storage.Database | ||
Validator *validator.Validate | ||
Logger *utility.Logger | ||
ExtReq request.ExternalRequest | ||
} | ||
|
||
func (base *Controller) CreateOrganisation(c *gin.Context) { | ||
|
||
var ( | ||
req = models.CreateOrgRequestModel{} | ||
) | ||
|
||
err := c.ShouldBind(&req) | ||
if err != nil { | ||
rd := utility.BuildErrorResponse(http.StatusBadRequest, "error", "Failed to parse request body", err, nil) | ||
c.JSON(http.StatusBadRequest, rd) | ||
return | ||
} | ||
|
||
err = base.Validator.Struct(&req) | ||
if err != nil { | ||
rd := utility.BuildErrorResponse(http.StatusUnprocessableEntity, "error", "Validation failed", utility.ValidationResponse(err, base.Validator), nil) | ||
c.JSON(http.StatusUnprocessableEntity, rd) | ||
return | ||
} | ||
|
||
reqData, code, err := service.ValidateCreateOrgRequest(req, base.Db.Postgresql) | ||
if err != nil { | ||
rd := utility.BuildErrorResponse(code, "error", err.Error(), err, nil) | ||
c.JSON(code, rd) | ||
return | ||
} | ||
|
||
claims, exists := c.Get("userClaims") | ||
if !exists { | ||
rd := utility.BuildErrorResponse(http.StatusBadRequest, "error", "unable to get user claims", err, nil) | ||
c.JSON(http.StatusBadRequest, rd) | ||
return | ||
} | ||
|
||
userClaims := claims.(jwt.MapClaims) | ||
|
||
userId := userClaims["user_id"].(string) | ||
|
||
respData, err := service.CreateOrganisation(reqData, base.Db.Postgresql, userId) | ||
if err != nil { | ||
rd := utility.BuildErrorResponse(http.StatusBadRequest, "error", err.Error(), err, nil) | ||
c.JSON(http.StatusBadRequest, rd) | ||
return | ||
} | ||
|
||
base.Logger.Info("organisation created successfully") | ||
rd := utility.BuildSuccessResponse(http.StatusCreated, "organisation created successfully", respData) | ||
|
||
c.JSON(http.StatusCreated, rd) | ||
} |
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,94 @@ | ||
package user | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/go-playground/validator/v10" | ||
|
||
"github.com/hngprojects/hng_boilerplate_golang_web/external/request" | ||
"github.com/hngprojects/hng_boilerplate_golang_web/internal/models" | ||
"github.com/hngprojects/hng_boilerplate_golang_web/pkg/repository/storage" | ||
"github.com/hngprojects/hng_boilerplate_golang_web/services/user" | ||
"github.com/hngprojects/hng_boilerplate_golang_web/utility" | ||
) | ||
|
||
type Controller struct { | ||
Db *storage.Database | ||
Validator *validator.Validate | ||
Logger *utility.Logger | ||
ExtReq request.ExternalRequest | ||
} | ||
|
||
func (base *Controller) CreateUser(c *gin.Context) { | ||
|
||
var ( | ||
req = models.CreateUserRequestModel{} | ||
) | ||
|
||
err := c.ShouldBind(&req) | ||
if err != nil { | ||
rd := utility.BuildErrorResponse(http.StatusBadRequest, "error", "Failed to parse request body", err, nil) | ||
c.JSON(http.StatusBadRequest, rd) | ||
return | ||
} | ||
|
||
err = base.Validator.Struct(&req) | ||
if err != nil { | ||
rd := utility.BuildErrorResponse(http.StatusUnprocessableEntity, "error", "Validation failed", utility.ValidationResponse(err, base.Validator), nil) | ||
c.JSON(http.StatusUnprocessableEntity, rd) | ||
return | ||
} | ||
|
||
reqData, err := user.ValidateCreateUserRequest(req, base.Db.Postgresql) | ||
if err != nil { | ||
rd := utility.BuildErrorResponse(http.StatusBadRequest, "error", err.Error(), err, nil) | ||
c.JSON(http.StatusBadRequest, rd) | ||
return | ||
} | ||
|
||
respData, code, err := user.CreateUser(reqData, base.Db.Postgresql) | ||
if err != nil { | ||
rd := utility.BuildErrorResponse(http.StatusBadRequest, "error", err.Error(), err, nil) | ||
c.JSON(http.StatusBadRequest, rd) | ||
return | ||
} | ||
|
||
base.Logger.Info("user created successfully") | ||
rd := utility.BuildSuccessResponse(http.StatusCreated, "user created successfully", respData) | ||
|
||
c.JSON(code, rd) | ||
} | ||
|
||
func (base *Controller) LoginUser(c *gin.Context) { | ||
|
||
var ( | ||
req = models.LoginRequestModel{} | ||
) | ||
|
||
err := c.ShouldBind(&req) | ||
if err != nil { | ||
rd := utility.BuildErrorResponse(http.StatusBadRequest, "error", "Failed to parse request body", err, nil) | ||
c.JSON(http.StatusBadRequest, rd) | ||
return | ||
} | ||
|
||
err = base.Validator.Struct(&req) | ||
if err != nil { | ||
rd := utility.BuildErrorResponse(http.StatusBadRequest, "error", "Validation failed", utility.ValidationResponse(err, base.Validator), nil) | ||
c.JSON(http.StatusBadRequest, rd) | ||
return | ||
} | ||
|
||
respData, code, err := user.LoginUser(req, base.Db.Postgresql) | ||
if err != nil { | ||
rd := utility.BuildErrorResponse(code, "error", err.Error(), err, nil) | ||
c.JSON(http.StatusBadRequest, rd) | ||
return | ||
} | ||
|
||
base.Logger.Info("user login successfully") | ||
|
||
rd := utility.BuildSuccessResponse(http.StatusOK, "user login successfully", respData) | ||
c.JSON(http.StatusOK, rd) | ||
} |
Oops, something went wrong.