Skip to content

Commit

Permalink
model refactor and user relation complete
Browse files Browse the repository at this point in the history
  • Loading branch information
piyush7833 committed Jun 24, 2024
1 parent cbb0540 commit e74321d
Show file tree
Hide file tree
Showing 1,324 changed files with 2,212 additions and 629 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ jobs:
JWT_SECRET: ${{ secrets.JWT_SECRET }}
DB_URI: ${{ secrets.DB_URI }}
run: |
cd docker
echo $DOCKER_PASSWORD | docker login -u $DOCKER_USERNAME --password-stdin
docker build --build-arg JWT_SECRET=${JWT_SECRET} --build-arg DB_URI=${DB_URI} -t piyush7833/chat-api:latest .
docker push piyush7833/chat-api:latest
Expand Down
17 changes: 0 additions & 17 deletions .pre-commit-config.yaml

This file was deleted.

11 changes: 11 additions & 0 deletions config/Alias/tableAlias.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package alias

var mainAlias = "t"

func GetUserRelationAlias() map[string]string {
return map[string]string{
"userRelation": mainAlias,
"user": "u1",
"relatedUser": "u2",
}
}
4 changes: 3 additions & 1 deletion config/constants.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package config

import "time"

const RowsPerPageGenral = 20
const CtxTimeout = 5
const CtxTimeout = 5 * time.Second
9 changes: 9 additions & 0 deletions config/defaultColumns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package config

func GetUserDefaultColumns() []string {
return []string{"id", "name", "username", "email", "phone", "image", "createdAt"}
}

func GetDefaultUserRelationColumns() []string {
return []string{"id", "userId", "relatedUserId", "user_name", "related_user_name", "status", "createdAt", "updatedAt"}
}
14 changes: 14 additions & 0 deletions config/joinColumns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package config

func GetJoinUserRelationColumns(ogAlias string, userAlias string, relatedUserAlias string) map[string]string {
return map[string]string{
"id": ogAlias + ".id",
"userId": ogAlias + ".userId",
"user_name": userAlias + ".name",
"relatedUserId": ogAlias + ".relatedUserId",
"related_user_name": relatedUserAlias + ".name",
"status": ogAlias + ".status",
"createdAt": ogAlias + ".createdAt",
"updatedAt": ogAlias + ".updatedAt",
}
}
38 changes: 38 additions & 0 deletions config/validColumns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package config

func GetValidUserColumns() map[string]bool {
return map[string]bool{
"id": true,
"name": true,
"username": true,
"email": true,
"phone": true,
"image": true,
"lastSeen": true,
"isOnline": true,
"phoneVerified": true,
"emailVerified": true,
"createdAt": true,
}
}

func UpdateValidUserColumns() map[string]bool {
return map[string]bool{
"name": true,
"email": true,
"image": true,
"phone": true,
}
}
func GetValidUserRelationColumns() map[string]bool {
return map[string]bool{
"id": true,
"userId": true,
"user_name": true,
"relatedUserId": true,
"related_user_name": true,
"status": true,
"createdAt": true,
"updatedAt": true,
}
}
8 changes: 6 additions & 2 deletions controllers/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ func Signup(w http.ResponseWriter, r *http.Request) {
// Read the request body
var user types.SignupType
err := helpers.GetBodyInJson(r, &user)
if helpers.CheckNilErr(err, "Error in retriving body", w) {
if err != nil {
helpers.Error(w, 500, err.Error())
return
}
res, error := functions.SignUp(user)
Expand All @@ -24,9 +25,12 @@ func Signup(w http.ResponseWriter, r *http.Request) {
}

func SignIn(w http.ResponseWriter, r *http.Request) {
// fmt.Println("Signin")
var user types.SignInType
err := helpers.GetBodyInJson(r, &user)
if helpers.CheckNilErr(err, "Error in retriving body", w) {
// fmt.Println(user, "user from controller")
if err != nil {
helpers.Error(w, 500, err.Error())
return
}
res, error := functions.Signin(user, w)
Expand Down
90 changes: 0 additions & 90 deletions controllers/friend-request.go

This file was deleted.

11 changes: 0 additions & 11 deletions controllers/friends.go

This file was deleted.

100 changes: 100 additions & 0 deletions controllers/user-relation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package controllers

import (
"net/http"

"github.com/gorilla/context"
"github.com/piyush7833/Chat-Api/config"
"github.com/piyush7833/Chat-Api/functions"
"github.com/piyush7833/Chat-Api/helpers"
"github.com/piyush7833/Chat-Api/types"
)

func CreateUserRelations(w http.ResponseWriter, r *http.Request) {
JwtData := context.Get(r, "userId").(*helpers.Claims)
var relation types.CreateUserRelationType
err := helpers.GetBodyInJson(r, &relation)
if err != nil {
helpers.Error(w, 500, err.Error())
return
}
var senderId = JwtData.Id
var status = "pending"
if relation.RelatedUserId == nil {
helpers.Error(w, 500, "ReceiverId is required")
return
}
relation.UserId = &senderId
relation.Status = &status
res, error := functions.CreateUserRelation(relation)
if error.StatusCode != 0 {
// fmt.Println(error.StatusCode, error.Message, "error from fucntion")
helpers.Error(w, error.StatusCode, error.Message)
return
}
helpers.Success(w, 201, res, "User relation created sent successfully")
}

func UpdateUserRelations(w http.ResponseWriter, r *http.Request) {
JwtData := context.Get(r, "userId").(*helpers.Claims)
queryValues := r.URL.Query()
id := queryValues.Get("id")
var relation types.UpdateUserRelationType
err := helpers.GetBodyInJson(r, &relation)
if err != nil {
helpers.Error(w, 500, err.Error())
return
}
res, error := functions.UpdateUserRelation(relation, id, JwtData.Id)
if error.StatusCode != 0 {
helpers.Error(w, error.StatusCode, error.Message)
return
}
helpers.Success(w, 200, res, "Relation updated successfully")
}

func GetAllUserRelations(w http.ResponseWriter, r *http.Request) {
JwtData := context.Get(r, "userId").(*helpers.Claims)
queryValues := r.URL.Query()

status := helpers.ProcessQuerryParams(queryValues["status"], []string{"all"}, "string").(string)
relation_type := helpers.ProcessQuerryParams(queryValues["type"], []string{"all"}, "string").(string)
page := helpers.ProcessQuerryParams(queryValues["page"], []string{"0"}, "number").(int)
selectedColumns := helpers.ProcessQuerryParams(queryValues["columns"], config.GetDefaultUserRelationColumns(), "array").([]string)
orderBy := helpers.ProcessQuerryParams(queryValues["orderBy"], []string{"createdAt"}, "array").([]string)
isDesc := helpers.ProcessQuerryParams(queryValues["isDesc"], []string{"true"}, "bool").(bool)

res, error := functions.GetAllUserRelations(status, page, selectedColumns, JwtData.Id, relation_type, orderBy, isDesc)
if error.StatusCode != 0 {
helpers.Error(w, error.StatusCode, error.Message)
return
}
helpers.Success(w, 200, res, "Relations fetched successfully")
}

func GetParticularUserRelations(w http.ResponseWriter, r *http.Request) {
JwtData := context.Get(r, "userId").(*helpers.Claims)
queryValues := r.URL.Query()

id := helpers.ProcessQuerryParams(queryValues["id"], []string{""}, "string").(string)
var selectedColumns = helpers.ProcessQuerryParams(queryValues["columns"], config.GetDefaultUserRelationColumns(), "array").([]string)

res, error := functions.GetParticularUserRelation(id, selectedColumns, JwtData.Id)
if error.StatusCode != 0 {
helpers.Error(w, error.StatusCode, error.Message)
return
}
helpers.Success(w, 200, res, "Relation fetched successfully")
}

func DeleteUserRelations(w http.ResponseWriter, r *http.Request) { //unrelate
JwtData := context.Get(r, "userId").(*helpers.Claims)
queryValues := r.URL.Query()
id := queryValues.Get("id")
res, error := functions.DeleteUserRelations(id, JwtData.Id)
if error.StatusCode != 0 {
helpers.Error(w, error.StatusCode, error.Message)
return
}
helpers.Success(w, 200, res, "Relation deleted successfully")
}
Loading

0 comments on commit e74321d

Please sign in to comment.