Skip to content

Commit

Permalink
adding user detail api
Browse files Browse the repository at this point in the history
  • Loading branch information
asyrawih committed Feb 20, 2021
1 parent 4abda67 commit 12a3c4d
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 15 deletions.
8 changes: 4 additions & 4 deletions controller/user_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func NewUserController(userService services.UserService) UserController {
func (controller *UserController) Route(app *fiber.App){
app.Post("/api/users" , controller.Create)
app.Get("/api/users" , controller.GetAll)
app.Get("/api/users/:id" , controller.GetAll)
}

func (controller *UserController) Create(ctx *fiber.Ctx) error {
Expand All @@ -39,12 +40,11 @@ func (controller *UserController) Create(ctx *fiber.Ctx) error {
}

func (controller UserController) GetAll(ctx *fiber.Ctx) error {
var response = controller.UserService.List()
id := ctx.Params("id")
var response = controller.UserService.List(id)
return ctx.JSON(model.WebResponse{
Code: 200,
Message: "GOT",
Data: &response,
})
}


}
1 change: 1 addition & 0 deletions exception/error_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

func ErrorHandler (ctx *fiber.Ctx, err error) error {
_ , oke := err.(ValidationError)

if oke {
return ctx.JSON(model.WebResponse{
Code: 400,
Expand Down
5 changes: 2 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,16 @@ import (
)

func main() {

// Setup Database
configuration := config.New()
database := config.NewMongoDatabase(configuration)

// Setup repository
userRepository := repository.NewUserRepository(database)

//For ignore variable
_ = repository.NewProductRepository(database)
// Setup Service
userService := service_user.NewUserService(&userRepository)

//Setup Controller
userController := controller.NewUserController(userService)

Expand Down
13 changes: 13 additions & 0 deletions repository/product_repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package repository

import "github.com/hananloser/rest-fiber-mongo-2/entity"

type ProductRepository interface {
Insert(product entity.Product)

GetProduct(products []entity.Product)

Find(id int)

DeleteAll()
}
30 changes: 30 additions & 0 deletions repository/product_repository_impl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package repository

import (
"github.com/hananloser/rest-fiber-mongo-2/entity"
"go.mongodb.org/mongo-driver/mongo"
)

type ProductRepositoryImpl struct {
Collection *mongo.Collection
}

func (p ProductRepositoryImpl) Insert(product entity.Product) {
panic("implement me")
}

func (p ProductRepositoryImpl) GetProduct(products []entity.Product) {
panic("implement me")
}

func (p ProductRepositoryImpl) Find(id int) {
panic("implement me")
}

func (p ProductRepositoryImpl) DeleteAll() {
panic("implement me")
}

func NewProductRepository(database *mongo.Database) ProductRepository {
return &ProductRepositoryImpl{Collection: database.Collection("product")}
}
9 changes: 5 additions & 4 deletions repository/user_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package repository
import "github.com/hananloser/rest-fiber-mongo-2/entity"

type UserRepository interface {
//Insert User
Insert(user entity.User)

GetAll() (user []entity.User)

// GetAll user
GetAll(userId string) (user []entity.User)
//Find User
Find(Id string) (user entity.User)

//DeleteAll User
DeleteAll()
}
7 changes: 6 additions & 1 deletion repository/user_repository_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,15 @@ func (u UserRepositoryImpl) Insert(user entity.User) {
}

// Get All User collection
func (u UserRepositoryImpl) GetAll() (users []entity.User) {
func (u UserRepositoryImpl) GetAll(userId string) (users []entity.User) {
ctx, cancel := config.NewMongoContext()
defer cancel()
query := bson.M{}

if userId != "" {
query = bson.M{"_id" : userId}
}

cursor, err := u.Collection.Find(ctx, query)
exception.PanicIfNeeded(err)

Expand Down
Binary file renamed main → rest-fiber-mongo-2
Binary file not shown.
15 changes: 15 additions & 0 deletions service/product/service_product.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package product

import "github.com/hananloser/rest-fiber-mongo-2/model"

// In Must Implements As Interface

type ServiceProduct interface {
// Inserting Product
// return model.CreateProductResponse
CreateProduct(product model.CreateProductRequest) model.CreateProductResponse

FindProduct()

DeleteProduct(id int)
}
5 changes: 4 additions & 1 deletion service/user/user_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package service_user
import "github.com/hananloser/rest-fiber-mongo-2/model"

type UserService interface {

// Create User
Create(request model.CreateUserRequest) (response model.CreateUserResponse)
// List User
List() (response []model.GetUserResponse)
List(userId string) (response []model.GetUserResponse)

Find(id string) (response model.FindUserResponse)
}
20 changes: 18 additions & 2 deletions service/user/user_service_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ type ServiceImpl struct {
UserRepository repository.UserRepository
}



func NewUserService(userRepository *repository.UserRepository) UserService {
return &ServiceImpl{
UserRepository: *userRepository,
Expand Down Expand Up @@ -43,8 +45,8 @@ func (userService ServiceImpl) Create(request model.CreateUserRequest) (response
return response
}

func (userService *ServiceImpl) List() (response []model.GetUserResponse) {
users := userService.UserRepository.GetAll()
func (userService *ServiceImpl) List(userId string) (response []model.GetUserResponse) {
users := userService.UserRepository.GetAll(userId)
for _, user := range users {
response = append(response, model.GetUserResponse{
Id: user.Id,
Expand All @@ -57,3 +59,17 @@ func (userService *ServiceImpl) List() (response []model.GetUserResponse) {
}
return response
}

func (userService ServiceImpl) Find(id string) (response model.FindUserResponse) {
var user = userService.UserRepository.Find(id)
response = model.FindUserResponse{
Id: user.Id,
Name: user.Name,
Email: user.Email,
Address: user.Address,
PhoneNumber: user.PhoneNumber,
}

return response

}

0 comments on commit 12a3c4d

Please sign in to comment.