Skip to content

Instantly share code, notes, and snippets.

@audrenbdb
Last active June 27, 2023 05:26
Show Gist options
  • Save audrenbdb/94660f707a206d385c42f64ceb93a4aa to your computer and use it in GitHub Desktop.
Save audrenbdb/94660f707a206d385c42f64ceb93a4aa to your computer and use it in GitHub Desktop.
package repository
import (
"context"
"github.com/ramadoiranedar/go_restapi/model/domain"
)
type CategoryRepository interface {
Create(ctx context.Context, category domain.Category) domain.Category
Update(ctx context.Context, category domain.Category) domain.Category
Delete(ctx context.Context, category domain.Category)
FindById(ctx context.Context, categoryId int) (domain.Category, error)
FindAll(ctx context.Context) []domain.Category
}
package repository
import (
"context"
"database/sql"
"github.com/ramadoiranedar/go_restapi/helper"
"github.com/ramadoiranedar/go_restapi/model/domain"
)
type CategoryRepositoryImpl struct {
db *sql.DB
}
func NewCategoryRepository(db *sql.DB) CategoryRepository {
return &CategoryRepositoryImpl{db: db}
}
func (repository *CategoryRepositoryImpl) Create(ctx context.Context, category domain.Category) domain.Category {
q := "insert into category (name) values (?)"
result, err := repository.db.ExecContext(ctx, q, category.Name)
helper.PanicIfError(err)
id, err := result.LastInsertId()
if err != nil {
panic(err)
}
category.Id = int(id)
return category
}
func (repository *CategoryRepositoryImpl) Update(ctx context.Context, category domain.Category) domain.Category {
_, err := repository.db.ExecContext(ctx, "UPDATE category SET name = ? WHERE id = ?", category.Name, category.Id)
helper.PanicIfError(err)
return category
}
func (repository *CategoryRepositoryImpl) Delete(ctx context.Context, category domain.Category) {
q := "delete from category where id = ?"
_, err := repository.db.ExecContext(ctx, q, category.Id)
helper.PanicIfError(err)
}
func (repository *CategoryRepositoryImpl) FindById(ctx context.Context, categoryId int) (domain.Category, error) {
q := "select id, name from category where id = ?"
row := repository.db.QueryRowContext(ctx, q, categoryId)
category := domain.Category{}
err := row.Scan(&category.Id, &category.Name)
return category, err
}
func (repository *CategoryRepositoryImpl) FindAll(ctx context.Context) []domain.Category {
q := "select id, name from category"
rows, err := repository.db.QueryContext(ctx, q)
helper.PanicIfError(err)
var categories []domain.Category
for rows.Next() {
category := domain.Category{}
err := rows.Scan(
&category.Id,
&category.Name,
)
helper.PanicIfError(err)
categories = append(categories, category)
}
return categories
}
package service
import (
"context"
"github.com/go-playground/validator"
"github.com/ramadoiranedar/go_restapi/exception"
"github.com/ramadoiranedar/go_restapi/helper"
"github.com/ramadoiranedar/go_restapi/model/domain"
"github.com/ramadoiranedar/go_restapi/model/web"
"github.com/ramadoiranedar/go_restapi/repository"
)
type CategoryServiceImpl struct {
CategoryRepository repository.CategoryRepository
Validate *validator.Validate
}
func NewCategoryService(categoryRepository repository.CategoryRepository, validate *validator.Validate) CategoryService {
return &CategoryServiceImpl{
CategoryRepository: categoryRepository,
Validate: validate,
}
}
func (service *CategoryServiceImpl) Create(ctx context.Context, request web.CategoryCreateRequest) web.CategoryResponse {
err := service.Validate.Struct(request)
helper.PanicIfError(err)
category := domain.Category{
Name: request.Name,
}
category = service.CategoryRepository.Create(ctx, category)
return helper.ToCategoryResponse(category)
}
func (service *CategoryServiceImpl) Update(ctx context.Context, request web.CategoryUpdateRequest) web.CategoryResponse {
err := service.Validate.Struct(request)
helper.PanicIfError(err)
category, err := service.CategoryRepository.FindById(ctx, request.Id)
if err != nil {
helper.PanicIfError(err)
}
category.Name = request.Name
category = service.CategoryRepository.Update(ctx, category)
return helper.ToCategoryResponse(category)
}
func (service *CategoryServiceImpl) Delete(ctx context.Context, categoryId int) {
category, err := service.CategoryRepository.FindById(ctx, categoryId)
if err != nil {
panic(exception.NewNotFoundError(err.Error()))
}
service.CategoryRepository.Delete(ctx, category)
}
func (service *CategoryServiceImpl) FindById(ctx context.Context, categoryId int) web.CategoryResponse {
category, err := service.CategoryRepository.FindById(ctx, categoryId)
if err != nil {
panic(exception.NewNotFoundError(err.Error()))
}
return helper.ToCategoryResponse(category)
}
func (service *CategoryServiceImpl) FindAll(ctx context.Context) []web.CategoryResponse {
categories := service.CategoryRepository.FindAll(ctx)
return helper.ToCategoryResponses(categories)
}
package main
import (
"github.com/ramadoiranedar/go_restapi/middleware"
"net/http"
"github.com/go-playground/validator"
_ "github.com/go-sql-driver/mysql"
"github.com/julienschmidt/httprouter"
"github.com/ramadoiranedar/go_restapi/app"
"github.com/ramadoiranedar/go_restapi/controller"
"github.com/ramadoiranedar/go_restapi/exception"
"github.com/ramadoiranedar/go_restapi/helper"
"github.com/ramadoiranedar/go_restapi/repository"
"github.com/ramadoiranedar/go_restapi/service"
)
func main() {
db := app.NewDB()
validate := validator.New()
categoryRepository := repository.NewCategoryRepository(db)
categroyService := service.NewCategoryService(categoryRepository, validate)
categoryController := controller.NewCategoryController(categroyService)
router := httprouter.New()
router.GET("/api/categories", categoryController.FindAll)
router.GET("/api/categories/:categoryId", categoryController.FindById)
router.POST("/api/categories", categoryController.Create)
router.PUT("/api/categories/:categoryId", categoryController.Update)
router.DELETE("/api/categories/:categoryId", categoryController.Delete)
router.PanicHandler = exception.ErrorHandler
server := http.Server{
Addr: "localhost:3000",
Handler: middleware.NewAuthMiddleware(router),
}
err := server.ListenAndServe()
helper.PanicIfError(err)
}
@congquyen01
Copy link

Verify Github on Galxe. gid:puCE8Nt4yaepayzppALd45

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment