-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4a64dd1
commit 0c1b48c
Showing
11 changed files
with
648 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module testBeego | ||
|
||
go 1.21.0 | ||
|
||
require ( | ||
gorm.io/driver/postgres v1.5.4 | ||
gorm.io/gorm v1.25.5 | ||
) | ||
|
||
require ( | ||
github.com/astaxie/beego v1.12.3 // indirect | ||
github.com/beorn7/perks v1.0.1 // indirect | ||
github.com/cespare/xxhash/v2 v2.1.1 // indirect | ||
github.com/golang/protobuf v1.4.2 // indirect | ||
github.com/hashicorp/golang-lru v0.5.4 // indirect | ||
github.com/jackc/pgpassfile v1.0.0 // indirect | ||
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect | ||
github.com/jackc/pgx/v5 v5.4.3 // indirect | ||
github.com/jinzhu/inflection v1.0.0 // indirect | ||
github.com/jinzhu/now v1.1.5 // indirect | ||
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect | ||
github.com/prometheus/client_golang v1.7.0 // indirect | ||
github.com/prometheus/client_model v0.2.0 // indirect | ||
github.com/prometheus/common v0.10.0 // indirect | ||
github.com/prometheus/procfs v0.1.3 // indirect | ||
github.com/shiena/ansicolor v0.0.0-20151119151921-a422bbe96644 // indirect | ||
golang.org/x/crypto v0.14.0 // indirect | ||
golang.org/x/net v0.10.0 // indirect | ||
golang.org/x/sys v0.13.0 // indirect | ||
golang.org/x/text v0.13.0 // indirect | ||
google.golang.org/protobuf v1.23.0 // indirect | ||
gopkg.in/yaml.v2 v2.2.8 // indirect | ||
) |
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,152 @@ | ||
package handlers | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strconv" | ||
"testBeego/helpers" | ||
"testBeego/models" | ||
"testBeego/repository" | ||
|
||
"github.com/astaxie/beego/context" | ||
) | ||
|
||
type HandlerController struct { | ||
Repo repository.RepoOperation | ||
} | ||
|
||
func HandlersInstance(repo repository.RepoOperation) *HandlerController { | ||
return &HandlerController{ | ||
Repo: repo, | ||
} | ||
} | ||
|
||
func (h HandlerController) CreateEmployee(ctx *context.Context) { | ||
var employee models.Employee | ||
|
||
ctx.ResponseWriter.Header().Add("content-type", "application/json") | ||
|
||
if err := json.NewDecoder(ctx.Request.Body).Decode(&employee); err != nil { | ||
ctx.ResponseWriter.WriteHeader(400) | ||
|
||
ctx.Output.JSON(struct { | ||
Code int `json:"Code"` | ||
Status string `json:"Status"` | ||
Error string `json:"Error"` | ||
}{400, "Invalid input", err.Error()}, true, true) | ||
return | ||
} | ||
|
||
response, err := h.Repo.InserEmp(employee) | ||
|
||
if helpers.CheckResponse(ctx, response, err) { | ||
ctx.Output.JSON(response, true, true) | ||
} | ||
} | ||
|
||
func (h HandlerController) GetAllEmployee(ctx *context.Context) { | ||
ctx.ResponseWriter.Header().Add("content-type", "application/json") | ||
|
||
response, err := h.Repo.GetAll() | ||
if err != nil { | ||
ctx.ResponseWriter.WriteHeader(500) | ||
|
||
ctx.Output.JSON(struct { | ||
Code int `json:"Code"` | ||
Message string `json:"Message"` | ||
Error string `json:"Error"` | ||
}{500, "Failed to fetch all employee data", err.Error()}, true, true) | ||
|
||
return | ||
} | ||
|
||
ctx.Output.JSON(response, true, true) | ||
} | ||
|
||
func (h HandlerController) GetEmployeeById(ctx *context.Context) { | ||
ctx.ResponseWriter.Header().Add("content-type", "application/json") | ||
|
||
id, err := strconv.Atoi(ctx.Input.Param(":id")) | ||
if err != nil { | ||
ctx.ResponseWriter.WriteHeader(400) | ||
|
||
ctx.Output.JSON(struct { | ||
Code int `json:"Code"` | ||
Status string `json:"Status"` | ||
Error string `json:"Error"` | ||
}{400, "Failed to parse the Id", err.Error()}, true, true) | ||
return | ||
} | ||
|
||
ID := uint(id) | ||
|
||
response, err := h.Repo.GetById(ID) | ||
if helpers.CheckResponse(ctx, response, err) { | ||
ctx.Output.JSON(response, true, true) | ||
} | ||
} | ||
|
||
func (h HandlerController) UpdateEmployeeById(ctx *context.Context) { | ||
fmt.Println("Check") | ||
ctx.ResponseWriter.Header().Add("content-type", "application/json") | ||
|
||
id, err := strconv.Atoi(ctx.Input.Param(":id")) | ||
if err != nil { | ||
ctx.ResponseWriter.WriteHeader(400) | ||
ctx.Output.JSON(struct { | ||
Code int `json:"Code"` | ||
Status string `json:"Status"` | ||
Error string `json:"Error"` | ||
}{400, "Failed to parse the Id", err.Error()}, true, true) | ||
return | ||
} | ||
|
||
ID := uint(id) | ||
|
||
var employee map[string]interface{} | ||
|
||
if err := json.NewDecoder(ctx.Request.Body).Decode(&employee); err != nil { | ||
ctx.ResponseWriter.WriteHeader(400) | ||
ctx.Output.JSON(struct { | ||
Code int `json:"Code"` | ||
Status string `json:"Status"` | ||
Error string `json:"Error"` | ||
}{400, "Invalid input", err.Error()}, true, true) | ||
return | ||
} | ||
|
||
response, err := h.Repo.UpdateById(ID, employee) | ||
if helpers.CheckResponse(ctx, response, err) { | ||
ctx.Output.JSON(response, true, true) | ||
} | ||
} | ||
|
||
func (h HandlerController) DeleteEmployeeById(ctx *context.Context) { | ||
fmt.Println("Check") | ||
ctx.ResponseWriter.Header().Add("content-type", "application/json") | ||
|
||
id, err := strconv.Atoi(ctx.Input.Param(":id")) | ||
if err != nil { | ||
ctx.ResponseWriter.WriteHeader(400) | ||
ctx.Output.JSON(struct { | ||
Code int `json:"Code"` | ||
Status string `json:"Status"` | ||
Error string `json:"Error"` | ||
}{400, "Failed to parse the Id", err.Error()}, true, true) | ||
return | ||
} | ||
|
||
ID := uint(id) | ||
|
||
response, err := h.Repo.DeleteById(ID) | ||
if err != nil { | ||
ctx.ResponseWriter.WriteHeader(400) | ||
ctx.Output.JSON(struct { | ||
Code int `json:"Code"` | ||
Status string `json:"Status"` | ||
Error string `json:"Error"` | ||
}{400, "Failed to parse the Id", err.Error()}, true, true) | ||
return | ||
} | ||
ctx.Output.JSON(response, true, true) | ||
} |
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,32 @@ | ||
package helpers | ||
|
||
import ( | ||
"testBeego/models" | ||
|
||
"github.com/astaxie/beego/context" | ||
) | ||
|
||
var ( | ||
Host = "localhost" | ||
Port = "5432" | ||
User = "postgres" | ||
Password = "password" | ||
Dbname = "beegoDB" | ||
) | ||
|
||
func CheckResponse(ctx *context.Context, res models.Resp, err error) bool { | ||
if err != nil { | ||
ctx.ResponseWriter.WriteHeader(res.StatusCode) | ||
|
||
ctx.Output.JSON(struct { | ||
Code int `json:"Code"` | ||
Message string `json:"Message"` | ||
Error string `json:"Error"` | ||
}{res.StatusCode, res.Message, err.Error()}, true, true) | ||
return false | ||
} | ||
|
||
ctx.ResponseWriter.WriteHeader(200) | ||
|
||
return true | ||
} |
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,14 @@ | ||
package main | ||
|
||
import ( | ||
"testBeego/repository" | ||
"testBeego/routers" | ||
) | ||
|
||
func main() { | ||
db := repository.DbConnection() | ||
repository.Migration(db) | ||
|
||
operation := repository.RepoController(db) | ||
routers.Router(operation) | ||
} |
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,24 @@ | ||
package models | ||
|
||
type Employee struct { | ||
ID uint `json:"-" gorm:"column:id"` | ||
Name string `json:"name" gorm:"column:name"` | ||
Addresses Address `json:"address" gorm:"foreignKey:EmpId;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"` | ||
} | ||
|
||
type Address struct { | ||
ID uint `json:"-" gorm:"column:id"` | ||
EmpId uint `json:"-"` | ||
City string `json:"city" gorm:"column:city"` | ||
State string `json:"state" gorm:"column:state"` | ||
Zip int `json:"zip" gorm:"column:zip"` | ||
PhoneNumber string `json:"phone_No" gorm:"column:phone_No"` | ||
} | ||
|
||
func (Employee) TableName() string { | ||
return "employee" | ||
} | ||
|
||
func (Address) TableName() string { | ||
return "address" | ||
} |
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,7 @@ | ||
package models | ||
|
||
type Resp struct { | ||
StatusCode int | ||
Message string | ||
Data interface{} | ||
} |
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,28 @@ | ||
package repository | ||
|
||
import ( | ||
"fmt" | ||
"testBeego/helpers" | ||
"testBeego/models" | ||
|
||
"gorm.io/driver/postgres" | ||
"gorm.io/gorm" | ||
) | ||
|
||
func DbConnection() *gorm.DB { | ||
connection := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", helpers.Host, helpers.Port, helpers.User, helpers.Password, helpers.Dbname) | ||
|
||
db, err := gorm.Open(postgres.Open(connection), &gorm.Config{}) | ||
if err != nil { | ||
fmt.Println("Db Connection is failed", err) | ||
return nil | ||
} | ||
|
||
fmt.Println("DB Drive Connected") | ||
|
||
return db | ||
} | ||
|
||
func Migration(db *gorm.DB) { | ||
db.AutoMigrate(&models.Employee{}, &models.Address{}) | ||
} |
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,19 @@ | ||
package repository | ||
|
||
import ( | ||
"testBeego/models" | ||
|
||
"gorm.io/gorm" | ||
) | ||
|
||
type RepoOperation interface { | ||
InserEmp(models.Employee) (models.Resp, error) | ||
GetAll() ([]models.Employee, error) | ||
GetById(id uint) (models.Resp, error) | ||
UpdateById(uint, map[string]interface{}) (models.Resp, error) | ||
DeleteById(id uint) (map[string]interface{}, error) | ||
} | ||
|
||
func RepoController(db *gorm.DB) RepoOperation { | ||
return &Controller{Db: db} | ||
} |
Oops, something went wrong.