diff --git a/common/firebase.go b/common/firebase.go index 68c64ed..c0b4d44 100644 --- a/common/firebase.go +++ b/common/firebase.go @@ -1,3 +1,5 @@ +// Package common - common packs +// References : https://github.com/firebase/firebase-admin-go package common import ( diff --git a/common/httpUtil.go b/common/httpUtil.go new file mode 100644 index 0000000..146af5d --- /dev/null +++ b/common/httpUtil.go @@ -0,0 +1,18 @@ +package common + +import "github.com/gin-gonic/gin" + +// NewHTTPError example +func NewHTTPError(ctx *gin.Context, status int, err error) { + er := HTTPError{ + Code: status, + Message: err.Error(), + } + ctx.JSON(status, er) +} + +// HTTPError example +type HTTPError struct { + Code int `json:"code" example:"400"` + Message string `json:"message" example:"Request Failure"` +} diff --git a/docs/docs.go b/docs/docs.go index 2c05337..a0286a6 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -29,38 +29,47 @@ var doc = `{ "host": "{{.Host}}", "basePath": "{{.BasePath}}", "paths": { - "/posts/create": { + "/users/login": { "post": { - "description": "Add Posts", + "description": "Login user by email and password", "consumes": [ "application/json" ], "produces": [ "application/json" ], - "summary": "Add Posts", + "tags": [ + "users" + ], + "summary": "Login user", "parameters": [ { - "type": "string", - "description": "name search by q", - "name": "q", - "in": "query" + "description": "Login user", + "name": "Body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/model.LoginUser" + } } ], "responses": { - "200": { - "description": "OK", + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/common.HTTPError" + } + }, + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/common.HTTPError" + } + }, + "500": { + "description": "Internal Server Error", "schema": { - "type": "array", - "items": { - "$ref": "#/definitions/model.Post" - } - }, - "headers": { - "Token": { - "type": "string", - "description": "qwerty" - } + "$ref": "#/definitions/common.HTTPError" } } } @@ -68,24 +77,33 @@ var doc = `{ } }, "definitions": { - "model.Post": { + "common.HTTPError": { + "type": "object", + "properties": { + "code": { + "type": "integer", + "example": 400 + }, + "message": { + "type": "string", + "example": "Request Failure" + } + } + }, + "model.LoginUser": { "type": "object", "required": [ - "Description", - "Title" + "Email", + "Password" ], "properties": { - "Description": { - "type": "string" - }, - "Title": { - "type": "string" - }, - "createdAt": { - "type": "string" + "Email": { + "type": "string", + "example": "email" }, - "id": { - "type": "string" + "Password": { + "type": "string", + "example": "password" } } } diff --git a/docs/swagger.json b/docs/swagger.json index 5a46413..a51cf9c 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -9,38 +9,47 @@ "license": {} }, "paths": { - "/posts/create": { + "/users/login": { "post": { - "description": "Add Posts", + "description": "Login user by email and password", "consumes": [ "application/json" ], "produces": [ "application/json" ], - "summary": "Add Posts", + "tags": [ + "users" + ], + "summary": "Login user", "parameters": [ { - "type": "string", - "description": "name search by q", - "name": "q", - "in": "query" + "description": "Login user", + "name": "Body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/model.LoginUser" + } } ], "responses": { - "200": { - "description": "OK", + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/common.HTTPError" + } + }, + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/common.HTTPError" + } + }, + "500": { + "description": "Internal Server Error", "schema": { - "type": "array", - "items": { - "$ref": "#/definitions/model.Post" - } - }, - "headers": { - "Token": { - "type": "string", - "description": "qwerty" - } + "$ref": "#/definitions/common.HTTPError" } } } @@ -48,24 +57,33 @@ } }, "definitions": { - "model.Post": { + "common.HTTPError": { + "type": "object", + "properties": { + "code": { + "type": "integer", + "example": 400 + }, + "message": { + "type": "string", + "example": "Request Failure" + } + } + }, + "model.LoginUser": { "type": "object", "required": [ - "Description", - "Title" + "Email", + "Password" ], "properties": { - "Description": { - "type": "string" - }, - "Title": { - "type": "string" - }, - "createdAt": { - "type": "string" + "Email": { + "type": "string", + "example": "email" }, - "id": { - "type": "string" + "Password": { + "type": "string", + "example": "password" } } } diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 1f22d66..0cfc6b6 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -1,17 +1,24 @@ definitions: - model.Post: + common.HTTPError: properties: - Description: + code: + example: 400 + type: integer + message: + example: Request Failure type: string - Title: - type: string - createdAt: + type: object + model.LoginUser: + properties: + Email: + example: email type: string - id: + Password: + example: password type: string required: - - Description - - Title + - Email + - Password type: object info: contact: @@ -20,28 +27,34 @@ info: url: http://readytowork.jp license: {} paths: - /posts/create: + /users/login: post: consumes: - application/json - description: Add Posts + description: Login user by email and password parameters: - - description: name search by q - in: query - name: q - type: string + - description: Login user + in: body + name: Body + required: true + schema: + $ref: '#/definitions/model.LoginUser' produces: - application/json responses: - "200": - description: OK - headers: - Token: - description: qwerty - type: string + "400": + description: Bad Request + schema: + $ref: '#/definitions/common.HTTPError' + "404": + description: Not Found + schema: + $ref: '#/definitions/common.HTTPError' + "500": + description: Internal Server Error schema: - items: - $ref: '#/definitions/model.Post' - type: array - summary: Add Posts + $ref: '#/definitions/common.HTTPError' + summary: Login user + tags: + - users swagger: "2.0" diff --git a/post/usecase/postUsecase.go b/post/usecase/postUsecase.go index c75847a..409bfbe 100644 --- a/post/usecase/postUsecase.go +++ b/post/usecase/postUsecase.go @@ -20,15 +20,7 @@ func GetPosts(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"data": &posts}) } -// AddPosts godoc -// @Summary Add Posts -// @Description Add Posts -// @Accept json -// @Produce json -// @Param q query string false "name search by q" -// @Success 200 {array} model.Post -// @Header 200 {string} Token "qwerty" -// @Router /posts/create [post] +// AddPosts add the post func AddPosts(c *gin.Context) { // for json type data we need to bind with the struct references https://mholt.github.io/json-to-go/ //JSON BIND diff --git a/tmp/runner-build b/tmp/runner-build index a059ef1..b2f439a 100755 Binary files a/tmp/runner-build and b/tmp/runner-build differ diff --git a/user/model/userModel.go b/user/model/userModel.go index 663c969..3e112c5 100644 --- a/user/model/userModel.go +++ b/user/model/userModel.go @@ -1,11 +1,17 @@ package model -// UserModel usermodel -type UserModel struct { - ID uint `gorm:"primary_key" json:"ID"` - Username string `gorm:"column:username" json:"Username" ` - Email string `gorm:"column:email;unique_index" json:"Email"` - Bio string `gorm:"column:bio;size:1024" json:"Bio"` - Image string `gorm:"column:image" json:"Image"` - Password string `gorm:"column:password;not null" json:"Password"` +// User usermodel +type User struct { + ID uint `json:"ID"` + Username string `json:"Username"` + Email string `json:"Email"` + Bio string `json:"Bio"` + Image string `json:"Image"` + Password string `json:"Password" ` +} + +// LoginUser userModel +type LoginUser struct { + Email string `json:"Email" example:"email" binding:"required"` + Password string `json:"Password" example:"password" binding:"required"` } diff --git a/user/repository/userRepp.go b/user/repository/userRepp.go new file mode 100644 index 0000000..5688e33 --- /dev/null +++ b/user/repository/userRepp.go @@ -0,0 +1,29 @@ +package repository + +import ( + "context" + "github/bhattaraibishal50/blog/common" + "log" +) + +// UserRepository interface +type UserRepository interface { + Login() +} + +type firebaseAuthRepo struct{} + +var fb = common.NewFirebaseApp() +var fbAuth = fb.GetFirebaseAuth() +var ctx = context.Background() + +// NewFirebaseAuthRepo constrictore returns user repos +func NewFirebaseAuthRepo() UserRepository { + return &firebaseAuthRepo{} +} + +// Login func +func (*firebaseAuthRepo) Login() { + usersRecord, err := fbAuth.GetUserByEmail(ctx, "bishal.bhattarai@readytowork.jp") + log.Printf("Successfully fetched user data: %v\n", *usersRecord, err) +} diff --git a/user/route/userRoute.go b/user/route/userRoute.go index 1fb8171..de6afdc 100644 --- a/user/route/userRoute.go +++ b/user/route/userRoute.go @@ -10,4 +10,5 @@ import ( func UserRoute(routerGroup *gin.RouterGroup) { routerGroup.GET("/", userUsecase.GetUsers) routerGroup.POST("/signup", userUsecase.Signup) + routerGroup.POST("/login", userUsecase.Login) } diff --git a/user/usecase/userUsecase.go b/user/usecase/userUsecase.go index 4c6173c..7b99af9 100644 --- a/user/usecase/userUsecase.go +++ b/user/usecase/userUsecase.go @@ -4,9 +4,13 @@ Package usecase is a business logics aread package usecase import ( + "github/bhattaraibishal50/blog/common" + "github/bhattaraibishal50/blog/user/model" + userRepo "github/bhattaraibishal50/blog/user/repository" "net/http" "github.com/gin-gonic/gin" + "github.com/gin-gonic/gin/binding" ) // GetUsers gets the users @@ -14,12 +18,30 @@ func GetUsers(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"status": "on users"}) } -// Login login the suer +// Login godoc +// @Summary Login user +// @Description Login user by email and password +// @Tags users +// @Accept json +// @Produce json +// @Param Body body userModel.LoginUser true "Login user" +// @Failure 400 {object} common.HTTPError +// @Failure 404 {object} common.HTTPError +// @Failure 500 {object} common.HTTPError +// @Router /users/login [post] func Login(c *gin.Context) { - c.JSON(http.StatusOK, gin.H{"status": "on login"}) + var loginUser model.LoginUser + err := c.ShouldBindBodyWith(&loginUser, binding.JSON) + if err != nil { + common.NewHTTPError(c, http.StatusBadRequest, err) + return + } + userRepo := userRepo.NewFirebaseAuthRepo() + userRepo.Login() + c.JSON(http.StatusOK, gin.H{"data": &loginUser}) } // Signup signup the user func Signup(c *gin.Context) { - //username + }