2022-12-31 10:22:38 +01:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2023-09-22 15:19:04 +02:00
|
|
|
|
2022-12-31 10:22:38 +01:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/go-playground/validator/v10"
|
2023-01-30 19:59:42 +01:00
|
|
|
"github.com/twofas/2fas-server/config"
|
|
|
|
"github.com/twofas/2fas-server/internal/api/icons/adapters"
|
|
|
|
"github.com/twofas/2fas-server/internal/api/icons/app"
|
|
|
|
"github.com/twofas/2fas-server/internal/api/icons/app/command"
|
|
|
|
"github.com/twofas/2fas-server/internal/api/icons/app/queries"
|
|
|
|
"github.com/twofas/2fas-server/internal/api/icons/ports"
|
|
|
|
"github.com/twofas/2fas-server/internal/common/aws"
|
|
|
|
"github.com/twofas/2fas-server/internal/common/db"
|
|
|
|
httpsec "github.com/twofas/2fas-server/internal/common/http"
|
|
|
|
"github.com/twofas/2fas-server/internal/common/storage"
|
2022-12-31 10:22:38 +01:00
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
type IconsModule struct {
|
|
|
|
Cqrs *app.Cqrs
|
|
|
|
RoutesHandler *ports.RoutesHandler
|
|
|
|
Config config.Configuration
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewIconsModule(config config.Configuration, gorm *gorm.DB, database *sql.DB, validate *validator.Validate) *IconsModule {
|
|
|
|
queryBuilder := db.NewQueryBuilder(database)
|
|
|
|
|
|
|
|
var iconsStorage storage.FileSystemStorage
|
|
|
|
|
|
|
|
if config.IsTestingEnv() {
|
|
|
|
iconsStorage = storage.NewTmpFileSystem()
|
|
|
|
} else {
|
|
|
|
iconsStorage = aws.NewAwsS3(config.Aws.Region, config.Aws.S3AccessKeyId, config.Aws.S3AccessSecretKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
webServicesRepository := adapters.NewWebServiceMysqlRepository(gorm)
|
|
|
|
iconsRepository := adapters.NewIconMysqlRepository(gorm)
|
|
|
|
iconsRelationsRepository := adapters.NewIconsRelationsMysqlRepository(gorm)
|
|
|
|
iconsRequestRepository := adapters.NewIconRequestMysqlRepository(gorm)
|
|
|
|
iconsCollectionRepository := adapters.NewIconsCollectionMysqlRepository(gorm)
|
|
|
|
iconsCollectionRelationsRepository := adapters.NewIconsCollectionsRelationsMysqlRepository(gorm)
|
|
|
|
|
|
|
|
cqrs := &app.Cqrs{
|
|
|
|
Commands: app.Commands{
|
|
|
|
CreateWebService: &command.CreateWebServiceHandler{Repository: webServicesRepository},
|
|
|
|
UpdateWebService: &command.UpdateWebServiceHandler{Repository: webServicesRepository},
|
|
|
|
RemoveWebService: &command.DeleteWebServiceHandler{Repository: webServicesRepository},
|
|
|
|
RemoveAllWebServices: &command.DeleteAllWebServicesHandler{
|
|
|
|
Database: gorm,
|
|
|
|
Qb: queryBuilder,
|
|
|
|
},
|
|
|
|
CreateIcon: &command.CreateIconHandler{Repository: iconsRepository, Storage: iconsStorage},
|
|
|
|
UpdateIcon: &command.UpdateIconHandler{Repository: iconsRepository, Storage: iconsStorage},
|
|
|
|
RemoveIcon: &command.DeleteIconHandler{
|
|
|
|
Repository: iconsRepository,
|
|
|
|
IconsRelationRepository: iconsRelationsRepository,
|
|
|
|
},
|
|
|
|
RemoveAllIcons: &command.DeleteAllIconsHandler{
|
|
|
|
Database: gorm,
|
|
|
|
Qb: queryBuilder,
|
|
|
|
},
|
|
|
|
CreateIconRequest: &command.CreateIconRequestHandler{
|
|
|
|
Storage: iconsStorage,
|
|
|
|
Repository: iconsRequestRepository,
|
|
|
|
},
|
|
|
|
RemoveIconRequest: &command.DeleteIconRequestHandler{
|
|
|
|
Repository: iconsRequestRepository,
|
|
|
|
},
|
|
|
|
RemoveAllIconsRequests: &command.DeleteAllIconsRequestsHandler{
|
|
|
|
Database: gorm,
|
|
|
|
Qb: queryBuilder,
|
|
|
|
},
|
|
|
|
UpdateWebServiceFromIconRequest: &command.UpdateWebServiceFromIconRequestHandler{
|
|
|
|
IconsStorage: iconsStorage,
|
|
|
|
WebServiceRepository: webServicesRepository,
|
|
|
|
IconsCollectionsRepository: iconsCollectionRepository,
|
|
|
|
IconsRequestsRepository: iconsRequestRepository,
|
|
|
|
IconsRepository: iconsRepository,
|
|
|
|
},
|
|
|
|
TransformIconRequestToWebService: &command.TransformIconRequestToWebServiceHandler{
|
|
|
|
IconsStorage: iconsStorage,
|
|
|
|
WebServiceRepository: webServicesRepository,
|
|
|
|
IconsRepository: iconsRepository,
|
|
|
|
IconsCollectionsRepository: iconsCollectionRepository,
|
|
|
|
IconsRequestsRepository: iconsRequestRepository,
|
|
|
|
},
|
|
|
|
CreateIconsCollection: &command.CreateIconsCollectionHandler{Repository: iconsCollectionRepository},
|
|
|
|
UpdateIconsCollection: &command.UpdateIconsCollectionHandler{Repository: iconsCollectionRepository},
|
|
|
|
RemoveIconsCollection: &command.DeleteIconsCollectionHandler{
|
|
|
|
Repository: iconsCollectionRepository,
|
|
|
|
IconsCollectionsRelationsRepository: iconsCollectionRelationsRepository,
|
|
|
|
},
|
|
|
|
RemoveAllIconsCollections: &command.DeleteAllIconsCollectionsHandler{
|
|
|
|
Database: gorm,
|
|
|
|
Qb: queryBuilder,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Queries: app.Queries{
|
|
|
|
WebServiceQuery: &queries.WebServiceQueryHandler{
|
|
|
|
Database: gorm,
|
|
|
|
Qb: queryBuilder,
|
|
|
|
},
|
|
|
|
WebServicesDumpQuery: &queries.WebServicesDumpQueryHandler{
|
|
|
|
Database: database,
|
|
|
|
},
|
|
|
|
IconQuery: &queries.IconQueryHandler{
|
|
|
|
Database: gorm,
|
|
|
|
Qb: queryBuilder,
|
|
|
|
},
|
|
|
|
IconsCollectionQuery: &queries.IconsCollectionQueryHandler{
|
|
|
|
Database: gorm,
|
|
|
|
Qb: queryBuilder,
|
|
|
|
},
|
|
|
|
IconRequestQuery: &queries.IconRequestQueryHandler{
|
|
|
|
Database: gorm,
|
|
|
|
Qb: queryBuilder,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
routesHandler := ports.NewRoutesHandler(cqrs, validate)
|
|
|
|
|
|
|
|
module := &IconsModule{
|
|
|
|
Cqrs: cqrs,
|
|
|
|
RoutesHandler: routesHandler,
|
|
|
|
Config: config,
|
|
|
|
}
|
|
|
|
|
|
|
|
return module
|
|
|
|
}
|
|
|
|
|
2023-10-13 21:03:41 +02:00
|
|
|
func (m *IconsModule) RegisterPublicRoutes(router *gin.Engine) {
|
2022-12-31 10:22:38 +01:00
|
|
|
publicRouter := router.Group("/")
|
|
|
|
|
|
|
|
publicRouter.GET("/mobile/web_services/:service_id", m.RoutesHandler.FindWebService)
|
|
|
|
publicRouter.GET("/mobile/web_services", m.RoutesHandler.FindAllWebServices)
|
|
|
|
|
|
|
|
publicRouter.GET("/mobile/web_services/dump", m.RoutesHandler.DumpWebServices)
|
|
|
|
|
|
|
|
publicRouter.GET("/mobile/icons/:icon_id", m.RoutesHandler.FindIcon)
|
|
|
|
publicRouter.GET("/mobile/icons", m.RoutesHandler.FindAllIcons)
|
|
|
|
|
|
|
|
publicRouter.GET("/mobile/icons/collections/:collection_id", m.RoutesHandler.FindIconsCollection)
|
|
|
|
publicRouter.GET("/mobile/icons/collections", m.RoutesHandler.FindAllIconsCollection)
|
|
|
|
|
2023-01-31 20:26:45 +01:00
|
|
|
publicRouter.
|
|
|
|
Use(httpsec.BodySizeLimitMiddleware(64*1000)).
|
|
|
|
POST("/mobile/icons/requests", m.RoutesHandler.CreateIconRequest)
|
2023-02-06 22:43:12 +01:00
|
|
|
|
|
|
|
publicRouter.GET("/mobile/icons/requests", m.RoutesHandler.FindAllIconsRequests)
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
2023-09-22 15:19:04 +02:00
|
|
|
|
|
|
|
func (m *IconsModule) RegisterAdminRoutes(g *gin.RouterGroup) {
|
|
|
|
// internal/admin
|
|
|
|
g.POST("/mobile/web_services", m.RoutesHandler.CreateWebService)
|
|
|
|
g.PUT("/mobile/web_services/:service_id", m.RoutesHandler.UpdateWebService)
|
|
|
|
g.DELETE("/mobile/web_services/:service_id", m.RoutesHandler.RemoveWebService)
|
|
|
|
|
|
|
|
if m.Config.IsTestingEnv() {
|
|
|
|
g.DELETE("/mobile/web_services", m.RoutesHandler.RemoveAllWebServices)
|
|
|
|
g.DELETE("/mobile/icons", m.RoutesHandler.RemoveAllIcons)
|
|
|
|
g.DELETE("/mobile/icons/collections", m.RoutesHandler.RemoveAllIconsCollections)
|
|
|
|
g.DELETE("/mobile/icons/requests", m.RoutesHandler.RemoveAllIconsRequests)
|
|
|
|
}
|
|
|
|
|
|
|
|
g.POST("/mobile/icons/collections", m.RoutesHandler.CreateIconsCollection)
|
|
|
|
g.PUT("/mobile/icons/collections/:collection_id", m.RoutesHandler.UpdateIconsCollection)
|
|
|
|
g.DELETE("/mobile/icons/collections/:collection_id", m.RoutesHandler.RemoveIconsCollection)
|
|
|
|
|
|
|
|
g.POST("/mobile/icons", m.RoutesHandler.CreateIcon)
|
|
|
|
g.PUT("/mobile/icons/:icon_id", m.RoutesHandler.UpdateIcon)
|
|
|
|
g.DELETE("/mobile/icons/:icon_id", m.RoutesHandler.RemoveIcon)
|
|
|
|
|
|
|
|
g.DELETE("/mobile/icons/requests/:icon_request_id", m.RoutesHandler.RemoveIconRequest)
|
|
|
|
g.POST("/mobile/icons/requests/:icon_request_id/commands/update_web_service", m.RoutesHandler.UpdateWebServiceFromIconRequest)
|
|
|
|
g.POST("/mobile/icons/requests/:icon_request_id/commands/transform_to_web_service", m.RoutesHandler.TransformToWebService)
|
|
|
|
g.GET("/mobile/icons/requests/:icon_request_id", m.RoutesHandler.FindIconRequest)
|
|
|
|
}
|