Skip to content

Commit

Permalink
Merge pull request #19 from go-park-mail-ru/task/PRI-8
Browse files Browse the repository at this point in the history
Task/pri 8
  • Loading branch information
vovangy authored Mar 6, 2024
2 parents 7bf9188 + d286daf commit aacc687
Show file tree
Hide file tree
Showing 13 changed files with 4,977 additions and 66 deletions.
Binary file added .DS_Store
Binary file not shown.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ dev-compose-up:

dev-compose-down:
$(DOCKER_COMPOSE) -f "dev-docker-compose.yaml" down

swagger:
swag init -g cmd/main/main.go
Binary file added cmd/.DS_Store
Binary file not shown.
57 changes: 34 additions & 23 deletions cmd/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,21 @@ import (
"syscall"
"time"

_ "2024_1_TeaStealers/docs"

"github.com/gorilla/mux"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
httpSwagger "github.com/swaggo/http-swagger"
)

// @title Sample Project API
// @version 1.0
// @description This is a sample server Tean server.

// @host 0.0.0.0:8080
// @BasePath /api
// @schemes http https
func main() {
_ = godotenv.Load()
db, err := sql.Open("postgres", fmt.Sprintf("postgres://%v:%v@%v:%v/%v?sslmode=disable",
Expand All @@ -44,6 +54,8 @@ func main() {
panic("failed to connect database" + err.Error())
}

http.HandleFunc("/docs/", httpSwagger.WrapHandler)

r := mux.NewRouter().PathPrefix("/api").Subrouter()
r.Use(middleware.CORSMiddleware)
r.HandleFunc("/ping", pingPongHandler).Methods(http.MethodGet)
Expand All @@ -62,44 +74,43 @@ func main() {
companyUsecase := companyUc.NewCompanyUsecase(companyRepo)
companyHandler := companyH.NewCompanyHandler(companyUsecase)

companyApi := r.PathPrefix("/company").Subrouter()
companyApi.Handle("/create", middleware.JwtMiddleware(http.HandlerFunc(companyHandler.CreateCompany))).Methods(http.MethodPost)
companyApi.HandleFunc("/get/by/id", companyHandler.GetCompanyById).Methods(http.MethodGet)
companyApi.HandleFunc("/get/list", companyHandler.GetCompaniesList).Methods(http.MethodGet)
companyApi.Handle("/delete/by/id", middleware.JwtMiddleware(http.HandlerFunc(companyHandler.DeleteCompanyById))).Methods(http.MethodDelete, http.MethodPost)
companyApi.Handle("/update/by/id", middleware.JwtMiddleware(http.HandlerFunc(companyHandler.UpdateCompanyById))).Methods(http.MethodPost, http.MethodPut)
companyApi := r.PathPrefix("/companies").Subrouter()
companyApi.HandleFunc("/", companyHandler.CreateCompany).Methods(http.MethodPost, http.MethodOptions)
companyApi.HandleFunc("/{id}", companyHandler.GetCompanyById).Methods(http.MethodGet, http.MethodOptions)
companyApi.HandleFunc("/list/", companyHandler.GetCompaniesList).Methods(http.MethodGet, http.MethodOptions)
companyApi.HandleFunc("/{id}", companyHandler.DeleteCompanyById).Methods(http.MethodDelete, http.MethodOptions)
companyApi.HandleFunc("/{id}", companyHandler.UpdateCompanyById).Methods(http.MethodPost, http.MethodOptions)

buildingRepo := buildingR.NewRepository(db)
buildingUsecase := buildingUc.NewBuildingUsecase(buildingRepo)
buildingHandler := buildingH.NewBuildingHandler(buildingUsecase)

buildingApi := r.PathPrefix("/building").Subrouter()
buildingApi.Handle("/create", middleware.JwtMiddleware(http.HandlerFunc(buildingHandler.CreateBuilding))).Methods(http.MethodPost)
buildingApi.HandleFunc("/get/by/id", buildingHandler.GetBuildingById).Methods(http.MethodGet)
buildingApi.HandleFunc("/get/list", buildingHandler.GetBuildingsList).Methods(http.MethodGet)
buildingApi.Handle("/delete/by/id", middleware.JwtMiddleware(http.HandlerFunc(buildingHandler.DeleteBuildingById))).Methods(http.MethodDelete, http.MethodPost)
buildingApi.Handle("/update/by/id", middleware.JwtMiddleware(http.HandlerFunc(buildingHandler.UpdateBuildingById))).Methods(http.MethodPost, http.MethodPut)
buildingApi := r.PathPrefix("/buildings").Subrouter()
buildingApi.HandleFunc("/", buildingHandler.CreateBuilding).Methods(http.MethodPost, http.MethodOptions)
buildingApi.HandleFunc("/{id}", buildingHandler.GetBuildingById).Methods(http.MethodGet, http.MethodOptions)
buildingApi.HandleFunc("/list/", buildingHandler.GetBuildingsList).Methods(http.MethodGet, http.MethodOptions)
buildingApi.HandleFunc("/{id}", buildingHandler.DeleteBuildingById).Methods(http.MethodDelete, http.MethodOptions)
buildingApi.HandleFunc("/{id}", buildingHandler.UpdateBuildingById).Methods(http.MethodPost, http.MethodOptions)

advertRepo := advertR.NewRepository(db)
advertUsecase := advertUc.NewAdvertUsecase(advertRepo)
advertHandler := advertH.NewAdvertHandler(advertUsecase)

advertApi := r.PathPrefix("/advert").Subrouter()
advertApi.Handle("/create", middleware.JwtMiddleware(http.HandlerFunc(advertHandler.CreateAdvert))).Methods(http.MethodPost)
advertApi.HandleFunc("/get/by/id", advertHandler.GetAdvertById).Methods(http.MethodGet)
advertApi.HandleFunc("/get/list", advertHandler.GetAdvertsList).Methods(http.MethodGet, http.MethodOptions)
advertApi.HandleFunc("/get/list/with/images", advertHandler.GetAdvertsWithImages).Methods(http.MethodGet)
advertApi.Handle("/delete/by/id", middleware.JwtMiddleware(http.HandlerFunc(advertHandler.DeleteAdvertById))).Methods(http.MethodDelete, http.MethodPost)
advertApi.Handle("/update/by/id", middleware.JwtMiddleware(http.HandlerFunc(advertHandler.UpdateAdvertById))).Methods(http.MethodPost, http.MethodPut)
advertApi := r.PathPrefix("/adverts").Subrouter()
advertApi.HandleFunc("/", advertHandler.CreateAdvert).Methods(http.MethodPost, http.MethodOptions)
advertApi.HandleFunc("/{id}", advertHandler.GetAdvertById).Methods(http.MethodGet, http.MethodOptions)
advertApi.HandleFunc("/list/", advertHandler.GetAdvertsList).Methods(http.MethodGet, http.MethodOptions)
advertApi.HandleFunc("/{id}", advertHandler.DeleteAdvertById).Methods(http.MethodDelete, http.MethodOptions)
advertApi.HandleFunc("/{id}", advertHandler.UpdateAdvertById).Methods(http.MethodPost, http.MethodOptions)

imageRepo := imageR.NewRepository(db)
imageUsecase := imageUc.NewImageUsecase(imageRepo)
imageHandler := imageH.NewImageHandler(imageUsecase)

imageApi := r.PathPrefix("/image").Subrouter()
imageApi.Handle("/create", middleware.JwtMiddleware(http.HandlerFunc(imageHandler.CreateImage))).Methods(http.MethodPost)
imageApi.HandleFunc("/get/list/by/advert/id", imageHandler.GetImagesByAdvertId).Methods(http.MethodGet)
imageApi.Handle("/delete/by/id", middleware.JwtMiddleware(http.HandlerFunc(imageHandler.DeleteImageById))).Methods(http.MethodDelete, http.MethodPost)
imageApi := r.PathPrefix("/images").Subrouter()
imageApi.HandleFunc("/", imageHandler.CreateImage).Methods(http.MethodPost, http.MethodOptions)
imageApi.HandleFunc("/list/by/{advert_id}", imageHandler.GetImagesByAdvertId).Methods(http.MethodGet, http.MethodOptions)
imageApi.HandleFunc("/{id}", imageHandler.DeleteImageById).Methods(http.MethodDelete, http.MethodOptions)

srv := &http.Server{
Addr: ":8080",
Expand Down
Loading

0 comments on commit aacc687

Please sign in to comment.