api with functioning managment

This commit is contained in:
Fran Jurmanović
2025-02-05 00:26:12 +01:00
parent 954fe0ae82
commit 9118574203
26 changed files with 2017 additions and 55 deletions

View File

@@ -3,6 +3,7 @@ package repository
import (
"acc-server-manager/local/model"
"context"
"errors"
"gorm.io/gorm"
)
@@ -27,6 +28,9 @@ func NewApiRepository(db *gorm.DB) *ApiRepository {
func (as ApiRepository) GetFirst(ctx context.Context) *model.ApiModel {
db := as.db.WithContext(ctx)
apiModel := new(model.ApiModel)
db.First(&apiModel)
result := db.First(&apiModel)
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil
}
return apiModel
}

View File

@@ -0,0 +1,66 @@
package repository
import (
"acc-server-manager/local/model"
"context"
"errors"
"gorm.io/gorm"
)
type ConfigRepository struct {
db *gorm.DB
}
func NewConfigRepository(db *gorm.DB) *ConfigRepository {
return &ConfigRepository{
db: db,
}
}
// UpdateConfig
// Updates first row from Config table.
//
// Args:
// context.Context: Application context
// Returns:
// model.ConfigModel: Config object from database.
func (as ConfigRepository) UpdateFirst(ctx context.Context) *model.Config {
db := as.db.WithContext(ctx)
ConfigModel := new(model.Config)
db.First(&ConfigModel)
return ConfigModel
}
// UpdateAll
// Updates All rows from Config table.
//
// Args:
// context.Context: Application context
// Returns:
// model.ConfigModel: Config object from database.
func (as ConfigRepository) UpdateAll(ctx context.Context) *[]model.Config {
db := as.db.WithContext(ctx)
ConfigModel := new([]model.Config)
db.Find(&ConfigModel)
return ConfigModel
}
// UpdateConfig
// Updates Config row from Config table.
//
// Args:
// context.Context: Application context
// Returns:
// model.ConfigModel: Config object from database.
func (as ConfigRepository) UpdateConfig(ctx context.Context, body *model.Config) *model.Config {
db := as.db.WithContext(ctx)
existingConfig := new(model.Config)
result := db.Where("server_id=?", body.ServerID).Where("config_file=?", body.ConfigFile).First(existingConfig)
if !errors.Is(result.Error, gorm.ErrRecordNotFound) {
body.ID = existingConfig.ID
}
db.Save(body)
return body
}

View File

@@ -0,0 +1,88 @@
package repository
import (
"acc-server-manager/local/model"
"context"
"gorm.io/gorm"
)
type LookupRepository struct {
db *gorm.DB
}
func NewLookupRepository(db *gorm.DB) *LookupRepository {
return &LookupRepository{
db: db,
}
}
// GetTracks
// Gets Tracks rows from Lookup table.
//
// Args:
// context.Context: Application context
// Returns:
// model.LookupModel: Lookup object from database.
func (as LookupRepository) GetTracks(ctx context.Context) *[]model.Track {
db := as.db.WithContext(ctx)
TrackModel := new([]model.Track)
db.Find(&TrackModel)
return TrackModel
}
// GetCarModels
// Gets CarModels rows from Lookup table.
//
// Args:
// context.Context: Application context
// Returns:
// model.LookupModel: Lookup object from database.
func (as LookupRepository) GetCarModels(ctx context.Context) *[]model.CarModel {
db := as.db.WithContext(ctx)
CarModelModel := new([]model.CarModel)
db.Find(&CarModelModel)
return CarModelModel
}
// GetDriverCategories
// Gets DriverCategories rows from Lookup table.
//
// Args:
// context.Context: Application context
// Returns:
// model.LookupModel: Lookup object from database.
func (as LookupRepository) GetDriverCategories(ctx context.Context) *[]model.DriverCategory {
db := as.db.WithContext(ctx)
DriverCategoryModel := new([]model.DriverCategory)
db.Find(&DriverCategoryModel)
return DriverCategoryModel
}
// GetCupCategories
// Gets CupCategories rows from Lookup table.
//
// Args:
// context.Context: Application context
// Returns:
// model.LookupModel: Lookup object from database.
func (as LookupRepository) GetCupCategories(ctx context.Context) *[]model.CupCategory {
db := as.db.WithContext(ctx)
CupCategoryModel := new([]model.CupCategory)
db.Find(&CupCategoryModel)
return CupCategoryModel
}
// GetSessionTypes
// Gets SessionTypes rows from Lookup table.
//
// Args:
// context.Context: Application context
// Returns:
// model.LookupModel: Lookup object from database.
func (as LookupRepository) GetSessionTypes(ctx context.Context) *[]model.SessionType {
db := as.db.WithContext(ctx)
SessionTypesModel := new([]model.SessionType)
db.Find(&SessionTypesModel)
return SessionTypesModel
}

View File

@@ -11,4 +11,7 @@ import (
// *dig.Container: Dig Container
func InitializeRepositories(c *dig.Container) {
c.Provide(NewApiRepository)
c.Provide(NewServerRepository)
c.Provide(NewConfigRepository)
c.Provide(NewLookupRepository)
}

View File

@@ -0,0 +1,64 @@
package repository
import (
"acc-server-manager/local/model"
"context"
"errors"
"gorm.io/gorm"
)
type ServerRepository struct {
db *gorm.DB
}
func NewServerRepository(db *gorm.DB) *ServerRepository {
return &ServerRepository{
db: db,
}
}
// GetFirst
// Gets first row from Server table.
//
// Args:
// context.Context: Application context
// Returns:
// model.ServerModel: Server object from database.
func (as ServerRepository) GetFirst(ctx context.Context, serverId int) *model.Server {
db := as.db.WithContext(ctx)
ServerModel := new(model.Server)
db.Where("id=?", serverId).First(&ServerModel)
return ServerModel
}
// GetFirstByServiceName
// Gets first row from Server table.
//
// Args:
// context.Context: Application context
// Returns:
// model.ServerModel: Server object from database.
func (as ServerRepository) GetFirstByServiceName(ctx context.Context, serviceName string) *model.Server {
db := as.db.WithContext(ctx)
ServerModel := new(model.Server)
result := db.Where("service_name=?", serviceName).First(&ServerModel)
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil
}
return ServerModel
}
// GetAll
// Gets All rows from Server table.
//
// Args:
// context.Context: Application context
// Returns:
// model.ServerModel: Server object from database.
func (as ServerRepository) GetAll(ctx context.Context) *[]model.Server {
db := as.db.WithContext(ctx)
ServerModel := new([]model.Server)
db.Find(&ServerModel)
return ServerModel
}