205 lines
7.7 KiB
Go
205 lines
7.7 KiB
Go
package services
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"git.dynamicdiscord.de/malobeo/portal/internal"
|
|
"git.dynamicdiscord.de/malobeo/portal/openapi"
|
|
"github.com/gin-gonic/gin"
|
|
"os"
|
|
)
|
|
|
|
var (
|
|
AccessAuths AccessAuthService = AccessAuthService{}
|
|
)
|
|
|
|
type AccessAuthService struct{}
|
|
|
|
func (u *AccessAuthService) GetAll(c *gin.Context) ([]openapi.AccessAuthorizationResponse, error) {
|
|
apiClient := internal.GetApiClient(c)
|
|
|
|
resp, r, err := apiClient.AccessAuthAPI.GetAllAccessauthsApiV1AaGet(context.Background()).Execute()
|
|
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error when calling `AccessAuthAPI.GetAllAccessAuthAaGet``: %v\n", err)
|
|
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
|
|
return nil, err
|
|
}
|
|
|
|
fmt.Fprintf(os.Stdout, "Response from `AccessAuthAPI.GetAllAccessAuthAaGet`: %v\n", resp)
|
|
return resp, nil
|
|
}
|
|
|
|
func (u *AccessAuthService) GetById(c *gin.Context, id int32) (*openapi.AccessAuthorizationResponse, error) {
|
|
apiClient := internal.GetApiClient(c)
|
|
|
|
resp, r, err := apiClient.AccessAuthAPI.GetOneAccessauthApiV1AaAaIdGet(context.Background(), id).Execute()
|
|
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error when calling `AccessAuthAPI.GetOneAccessauthApiV1AaAaIdGet``: %v\n", err)
|
|
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
|
|
return nil, err
|
|
}
|
|
|
|
fmt.Fprintf(os.Stdout, "Response from `AccessAuthAPI.GetOneAccessauthApiV1AaAaIdGet`: %v\n", resp)
|
|
return resp, nil
|
|
}
|
|
|
|
func (u *AccessAuthService) Create(c *gin.Context, name string) (*openapi.AccessAuthorizationResponse, error) {
|
|
accessAuthorizationCreate := *openapi.NewAccessAuthorizationCreate(name, false, []openapi.TimetableCreate{})
|
|
apiClient := internal.GetApiClient(c)
|
|
|
|
resp, r, err := apiClient.AccessAuthAPI.AddAccessauthApiV1AaPost(context.Background()).AccessAuthorizationCreate(accessAuthorizationCreate).Execute()
|
|
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error when calling `AccessAuthAPI.CreateAccessAuthAccessAuthsPost``: %v\n", err)
|
|
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
|
|
return nil, err
|
|
}
|
|
|
|
fmt.Fprintf(os.Stdout, "Response from `AccessAuthAPI.CreateAccessAuthAccessAuthsPost`: %v\n", resp)
|
|
return resp, nil
|
|
}
|
|
|
|
func (u *AccessAuthService) Delete(c *gin.Context, id int32) error {
|
|
apiClient := internal.GetApiClient(c)
|
|
resp, r, err := apiClient.AccessAuthAPI.DeleteAccessauthApiV1AaAaIdDelete(context.Background(), id).Execute()
|
|
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error when calling `AccessAuthAPI.DeleteAccessauthApiV1AaAaIdDelete``: %v\n", err)
|
|
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
|
|
return err
|
|
}
|
|
|
|
fmt.Fprintf(os.Stdout, "Response from `AccessAuthAPI.DeleteAccessauthApiV1AaAaIdDelete`: %v\n", resp)
|
|
return nil
|
|
}
|
|
|
|
func (u *AccessAuthService) SetActive(c *gin.Context, accessAuthId int32, isActive bool) (*openapi.AccessAuthorizationResponse, error) {
|
|
apiClient := internal.GetApiClient(c)
|
|
accessAuthorizationUpdate := *openapi.NewAccessAuthorizationUpdate()
|
|
accessAuthorizationUpdate.IsActive = *openapi.NewNullableBool(&isActive)
|
|
|
|
resp, r, err := apiClient.AccessAuthAPI.ChangeAccessauthApiV1AaAaIdPatch(context.Background(), accessAuthId).AccessAuthorizationUpdate(accessAuthorizationUpdate).Execute()
|
|
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error when calling `AccessAuthAPI.ChangeAccessauthAaAaIdPatch``: %v\n", err)
|
|
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
|
|
return nil, err
|
|
}
|
|
|
|
fmt.Fprintf(os.Stdout, "Response from `AccessAuthAPI.ChangeAccessauthAaAaIdPatch`: %v\n", resp)
|
|
return resp, nil
|
|
}
|
|
|
|
func (u *AccessAuthService) SetName(c *gin.Context, accessAuthId int32, name string) (*openapi.AccessAuthorizationResponse, error) {
|
|
apiClient := internal.GetApiClient(c)
|
|
accessAuthorizationUpdate := *openapi.NewAccessAuthorizationUpdate()
|
|
accessAuthorizationUpdate.Name = *openapi.NewNullableString(&name)
|
|
|
|
resp, r, err := apiClient.AccessAuthAPI.ChangeAccessauthApiV1AaAaIdPatch(context.Background(), accessAuthId).AccessAuthorizationUpdate(accessAuthorizationUpdate).Execute()
|
|
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error when calling `AccessAuthAPI.ChangeAccessauthAaAaIdPatch``: %v\n", err)
|
|
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
|
|
return nil, err
|
|
}
|
|
|
|
fmt.Fprintf(os.Stdout, "Response from `AccessAuthAPI.ChangeAccessauthAaAaIdPatch`: %v\n", resp)
|
|
return resp, nil
|
|
}
|
|
|
|
func (u *AccessAuthService) SetTimetables(c *gin.Context, accessAuthId int32, timetables []openapi.TimetableCreate) (*openapi.AccessAuthorizationResponse, error) {
|
|
apiClient := internal.GetApiClient(c)
|
|
accessAuthorizationUpdate := *openapi.NewAccessAuthorizationUpdate()
|
|
accessAuthorizationUpdate.Timetables = timetables
|
|
resp, r, err := apiClient.AccessAuthAPI.ChangeAccessauthApiV1AaAaIdPatch(context.Background(), accessAuthId).AccessAuthorizationUpdate(accessAuthorizationUpdate).Execute()
|
|
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error when calling `AccessAuthAPI.ChangeAccessauthAaAaIdPatch``: %v\n", err)
|
|
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
|
|
return nil, err
|
|
}
|
|
|
|
fmt.Fprintf(os.Stdout, "Response from `AccessAuthAPI.ChangeAccessauthAaAaIdPatch`: %v\n", resp)
|
|
return resp, nil
|
|
}
|
|
|
|
func (u *AccessAuthService) AddTimetable(c *gin.Context, accessAuthId int32, weekDay int32, startTime string, duration int32) (*openapi.AccessAuthorizationResponse, error) {
|
|
apiClient := internal.GetApiClient(c)
|
|
currentAuthorizationAuth, err := u.GetById(c, accessAuthId)
|
|
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error when calling retreiving accessAuth by ID: %v\n", err)
|
|
return nil, err
|
|
}
|
|
|
|
newTimetables := []openapi.TimetableCreate{}
|
|
|
|
for _, timetable := range currentAuthorizationAuth.Timetables {
|
|
newTimetables = append(newTimetables, openapi.TimetableCreate{
|
|
Weekday: timetable.Weekday,
|
|
Starttime: timetable.Starttime,
|
|
Duration: timetable.Duration,
|
|
})
|
|
}
|
|
|
|
newTimetables = append(newTimetables, openapi.TimetableCreate{
|
|
Weekday: weekDay,
|
|
Starttime: startTime,
|
|
Duration: duration,
|
|
})
|
|
|
|
accessAuthorizationUpdate := *openapi.NewAccessAuthorizationUpdate()
|
|
accessAuthorizationUpdate.Timetables = newTimetables
|
|
|
|
resp, r, err := apiClient.AccessAuthAPI.ChangeAccessauthApiV1AaAaIdPatch(context.Background(), accessAuthId).AccessAuthorizationUpdate(accessAuthorizationUpdate).Execute()
|
|
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error when calling `AccessAuthAPI.ChangeAccessauthAaAaIdPatch``: %v\n", err)
|
|
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
|
|
return nil, err
|
|
}
|
|
|
|
fmt.Fprintf(os.Stdout, "Response from `AccessAuthAPI.ChangeAccessauthAaAaIdPatch`: %v\n", resp)
|
|
return resp, nil
|
|
}
|
|
|
|
func (u *AccessAuthService) RemoveTimetable(c *gin.Context, accessAuthId int32, timetableId int32) (*openapi.AccessAuthorizationResponse, error) {
|
|
apiClient := internal.GetApiClient(c)
|
|
currentAuthorizationAuth, err := u.GetById(c, accessAuthId)
|
|
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error when calling retreiving accessAuth by ID: %v\n", err)
|
|
return nil, err
|
|
}
|
|
|
|
newTimetables := []openapi.TimetableCreate{}
|
|
|
|
for _, timetable := range currentAuthorizationAuth.Timetables {
|
|
if timetable.Id == *openapi.NewNullableInt32(&timetableId) {
|
|
continue
|
|
}
|
|
|
|
newTimetables = append(newTimetables, openapi.TimetableCreate{
|
|
Weekday: timetable.Weekday,
|
|
Starttime: timetable.Starttime,
|
|
Duration: timetable.Duration,
|
|
})
|
|
}
|
|
|
|
accessAuthorizationUpdate := *openapi.NewAccessAuthorizationUpdate()
|
|
accessAuthorizationUpdate.Timetables = newTimetables
|
|
|
|
resp, r, err := apiClient.AccessAuthAPI.ChangeAccessauthApiV1AaAaIdPatch(context.Background(), accessAuthId).AccessAuthorizationUpdate(accessAuthorizationUpdate).Execute()
|
|
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error when calling `AccessAuthAPI.ChangeAccessauthAaAaIdPatch``: %v\n", err)
|
|
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
|
|
return nil, err
|
|
}
|
|
|
|
fmt.Fprintf(os.Stdout, "Response from `AccessAuthAPI.ChangeAccessauthAaAaIdPatch`: %v\n", resp)
|
|
return resp, nil
|
|
}
|