Add project
This commit is contained in:
156
Api/Services/Implementation/SeasonService.cs
Normal file
156
Api/Services/Implementation/SeasonService.cs
Normal file
@@ -0,0 +1,156 @@
|
||||
using Api.Database;
|
||||
using Api.Database.Entities;
|
||||
using Api.Extensions;
|
||||
using Api.Mapping;
|
||||
using Api.Services.Interfaces;
|
||||
using Common.Dtos.Common;
|
||||
using Common.Dtos.Season;
|
||||
using Common.Extensions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Caching.Distributed;
|
||||
|
||||
namespace Api.Services.Implementation;
|
||||
|
||||
public class SeasonService(SanStudentContext context, IDistributedCache cache) : ISeasonService, IServiceHelper<CreateSeasonRequest>
|
||||
{
|
||||
private readonly SanStudentContext _context = context;
|
||||
private readonly IDistributedCache _cache = cache;
|
||||
private List<string> _cacheKeys = [];
|
||||
|
||||
public async Task<CreateSeasonResponse> CreateAsync(CreateSeasonRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
var problems = await Validate(request);
|
||||
if (problems.Errors.Count > 0)
|
||||
{
|
||||
return new CreateSeasonResponse
|
||||
{
|
||||
Ok = false,
|
||||
CreateSeasonResult = string.Join(", ", problems.Errors)
|
||||
};
|
||||
}
|
||||
|
||||
var season = new Season()
|
||||
{
|
||||
Name = request.Name!.Trim(),
|
||||
StartDate = request.StartDate,
|
||||
EndDate = request.EndDate
|
||||
};
|
||||
|
||||
_context.Seasons.Add(season);
|
||||
await _context.SaveChangesAsync();
|
||||
await RemoveCache();
|
||||
return new CreateSeasonResponse
|
||||
{
|
||||
Ok = true,
|
||||
CreateSeasonResult = $"Zapisano nowy semestr \"{season.Name}\"",
|
||||
};
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
return new CreateSeasonResponse
|
||||
{
|
||||
Ok = false,
|
||||
CreateSeasonResult = $"Błąd podczas próby zapisu semestru: {error.Message}",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<SeasonDto[]> GetAllAsync()
|
||||
{
|
||||
var key = "seasons";
|
||||
AddKeyToCache(key);
|
||||
var cachedSeasons = await _cache.GetStringAsync(key);
|
||||
if (!string.IsNullOrEmpty(cachedSeasons))
|
||||
return cachedSeasons.FromJson<SeasonDto[]>();
|
||||
|
||||
var query = _context.Seasons as IQueryable<Season>;
|
||||
var result = await query
|
||||
.Where(s => s.EndDate > DateOnly.FromDateTime(DateTime.Now))
|
||||
.OrderBy(s => s.StartDate)
|
||||
.ThenBy(s => s.Name)
|
||||
.Select(s => s.ToSeasonDto()).ToArrayAsync();
|
||||
|
||||
await _cache.SetWithExpirationTimeAsync(key, result, 131487);
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<PagedList<SeasonDto>> GetAllPagedAsync(GetSeasonsRequest request)
|
||||
{
|
||||
var key = $"seasons_{request.PageNumber}_{request.PageSize}";
|
||||
if (!string.IsNullOrWhiteSpace(request.Name))
|
||||
key += $"_{request.Name.ToLower()}";
|
||||
|
||||
if (request.HideObsolete.HasValue && request.HideObsolete.Value)
|
||||
key += $"_{true}";
|
||||
|
||||
AddKeyToCache(key);
|
||||
var cachedSeasons = await _cache.GetStringAsync(key);
|
||||
if (!string.IsNullOrEmpty(cachedSeasons))
|
||||
return cachedSeasons.FromJson<PagedList<SeasonDto>>();
|
||||
|
||||
var query = _context.Seasons as IQueryable<Season>;
|
||||
|
||||
if (!string.IsNullOrEmpty(request.Name))
|
||||
query = query.Where(s => s.Name.ToLower().StartsWith(request.Name.ToLower()));
|
||||
|
||||
if (request.HideObsolete.HasValue && request.HideObsolete.Value)
|
||||
query = query.Where(s => s.EndDate > DateOnly.FromDateTime(DateTime.Now));
|
||||
|
||||
var data = query
|
||||
.OrderBy(s => s.StartDate)
|
||||
.ThenBy(s => s.Name)
|
||||
.Select(s => s.ToSeasonDto());
|
||||
|
||||
var result = await data.ToPagedListAsync(request.PageNumber, request.PageSize);
|
||||
await _cache.SetWithExpirationTimeAsync(key, result, 131487);
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
public async Task<ValidationProblems> Validate(CreateSeasonRequest request)
|
||||
{
|
||||
var problems = new ValidationProblems();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(request.Name))
|
||||
problems.Errors.Add("Brak nazwy semestru");
|
||||
|
||||
if (problems.Errors.Count > 0)
|
||||
return problems;
|
||||
|
||||
|
||||
var season = await _context.Seasons
|
||||
.FirstOrDefaultAsync(s => s.Name.ToLower() == request.Name!.ToLower());
|
||||
|
||||
if (season is not null)
|
||||
problems.Errors.Add("Semestr o podanej nazwie już istnieje");
|
||||
|
||||
season = await _context.Seasons
|
||||
.FirstOrDefaultAsync(s => s.StartDate == request.StartDate);
|
||||
|
||||
if (season is not null)
|
||||
problems.Errors.Add("Semestr o podanej dacie rozpoczęcia już istnieje");
|
||||
|
||||
season = await _context.Seasons
|
||||
.FirstOrDefaultAsync(s => s.EndDate == request.EndDate);
|
||||
|
||||
if (season is not null)
|
||||
problems.Errors.Add("Semestr o podanej dacie zakończenia już istnieje");
|
||||
|
||||
return problems;
|
||||
}
|
||||
|
||||
public async Task RemoveCache()
|
||||
{
|
||||
foreach (var cacheKey in _cacheKeys)
|
||||
await _cache.RemoveAsync(cacheKey);
|
||||
}
|
||||
|
||||
public void AddKeyToCache(string key)
|
||||
{
|
||||
if (!_cacheKeys.Contains(key))
|
||||
_cacheKeys.Add(key);
|
||||
}
|
||||
}
|
||||
|
||||
146
Api/Services/Implementation/SpecializationService.cs
Normal file
146
Api/Services/Implementation/SpecializationService.cs
Normal file
@@ -0,0 +1,146 @@
|
||||
using Api.Database;
|
||||
using Api.Database.Entities;
|
||||
using Api.Extensions;
|
||||
using Api.Mapping;
|
||||
using Api.Services.Interfaces;
|
||||
using Common.Dtos.Common;
|
||||
using Common.Dtos.Season;
|
||||
using Common.Dtos.Specialization;
|
||||
using Common.Extensions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Caching.Distributed;
|
||||
|
||||
namespace Api.Services.Implementation;
|
||||
|
||||
public class SpecializationService(SanStudentContext context, IDistributedCache cache)
|
||||
: ISpecializationService, IServiceHelper<CreateSpecializationRequest>
|
||||
{
|
||||
private readonly SanStudentContext _context = context;
|
||||
private readonly IDistributedCache _cache = cache;
|
||||
private List<string> _cacheKeys = [];
|
||||
|
||||
public async Task<CreateSpecializationResponse> CreateAsync(CreateSpecializationRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
var problems = await Validate(request);
|
||||
if (problems.Errors.Count > 0)
|
||||
{
|
||||
return new CreateSpecializationResponse
|
||||
{
|
||||
Ok = false,
|
||||
CreateSpecializationResult = string.Join(", ", problems.Errors)
|
||||
};
|
||||
}
|
||||
|
||||
var specialization = new Specialization()
|
||||
{
|
||||
Name = request.Name!.Trim(),
|
||||
ShortName = request.ShortName!.Trim()
|
||||
};
|
||||
|
||||
_context.Specializations.Add(specialization);
|
||||
await _context.SaveChangesAsync();
|
||||
await RemoveCache();
|
||||
return new CreateSpecializationResponse
|
||||
{
|
||||
Ok = true,
|
||||
CreateSpecializationResult = $"Zapisano nową specializację \"{specialization.Name}\"",
|
||||
};
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
return new CreateSpecializationResponse
|
||||
{
|
||||
Ok = false,
|
||||
CreateSpecializationResult = $"Błąd podczas próby zapisu specializacji: {error.Message}",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<SpecializationDto[]> GetAllAsync()
|
||||
{
|
||||
var key = "specializations";
|
||||
AddKeyToCache(key);
|
||||
var cachedSpecs = await _cache.GetStringAsync(key);
|
||||
if (!string.IsNullOrEmpty(cachedSpecs))
|
||||
return cachedSpecs.FromJson<SpecializationDto[]>();
|
||||
|
||||
var query = _context.Specializations as IQueryable<Specialization>;
|
||||
var result = await query
|
||||
.OrderBy(s => s.Name)
|
||||
.ThenBy(s => s.ShortName)
|
||||
.Select(s => s.ToSpecializationDto()).ToArrayAsync();
|
||||
|
||||
await _cache.SetWithExpirationTimeAsync(key, result, 262487);
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<PagedList<SpecializationDto>> GetAllPagedAsync(GetSpecializationsRequest request)
|
||||
{
|
||||
var key = $"specializations_{request.PageNumber}_{request.PageSize}";
|
||||
if (!string.IsNullOrWhiteSpace(request.Name))
|
||||
key += $"_{request.Name.ToLower()}";
|
||||
|
||||
AddKeyToCache(key);
|
||||
var cachedSpecs = await _cache.GetStringAsync(key);
|
||||
if (!string.IsNullOrEmpty(cachedSpecs))
|
||||
return cachedSpecs.FromJson<PagedList<SpecializationDto>>();
|
||||
|
||||
var query = _context.Specializations as IQueryable<Specialization>;
|
||||
|
||||
if (!string.IsNullOrEmpty(request.Name))
|
||||
query = query.Where(s => s.Name.ToLower().StartsWith(request.Name.ToLower()));
|
||||
|
||||
var data = query
|
||||
.OrderBy(s => s.Name)
|
||||
.ThenBy(s => s.ShortName)
|
||||
.Select(s => s.ToSpecializationDto());
|
||||
|
||||
var result = await data.ToPagedListAsync(request.PageNumber, request.PageSize);
|
||||
await _cache.SetWithExpirationTimeAsync(key, result, 262487);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
public async Task<ValidationProblems> Validate(CreateSpecializationRequest request)
|
||||
{
|
||||
var problems = new ValidationProblems();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(request.Name))
|
||||
problems.Errors.Add("Brak nazwy specializacji");
|
||||
|
||||
if (string.IsNullOrWhiteSpace(request.ShortName))
|
||||
problems.Errors.Add("Brak nazwy skróconej specializacji");
|
||||
|
||||
if (problems.Errors.Count > 0)
|
||||
return problems;
|
||||
|
||||
|
||||
var specialization = await _context.Specializations
|
||||
.FirstOrDefaultAsync(s => s.Name.ToLower() == request.Name!.ToLower());
|
||||
|
||||
if (specialization is not null)
|
||||
problems.Errors.Add("Specializacja o podanej nazwie już istnieje");
|
||||
|
||||
specialization = await _context.Specializations
|
||||
.FirstOrDefaultAsync(s => s.ShortName.ToLower() == request.ShortName!.ToLower());
|
||||
|
||||
if (specialization is not null)
|
||||
problems.Errors.Add("Specializacja o podanej nazwie skróconej już istnieje");
|
||||
|
||||
return problems;
|
||||
}
|
||||
|
||||
public async Task RemoveCache()
|
||||
{
|
||||
foreach (var cacheKey in _cacheKeys)
|
||||
await _cache.RemoveAsync(cacheKey);
|
||||
}
|
||||
|
||||
public void AddKeyToCache(string key)
|
||||
{
|
||||
if (!_cacheKeys.Contains(key))
|
||||
_cacheKeys.Add(key);
|
||||
}
|
||||
}
|
||||
144
Api/Services/Implementation/SubjectService.cs
Normal file
144
Api/Services/Implementation/SubjectService.cs
Normal file
@@ -0,0 +1,144 @@
|
||||
using Api.Database;
|
||||
using Api.Database.Entities;
|
||||
using Api.Extensions;
|
||||
using Api.Mapping;
|
||||
using Api.Services.Interfaces;
|
||||
using Common.Dtos.Common;
|
||||
using Common.Dtos.Subject;
|
||||
using Common.Extensions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Caching.Distributed;
|
||||
|
||||
namespace Api.Services.Implementation;
|
||||
|
||||
public class SubjectService(SanStudentContext context, IDistributedCache cache)
|
||||
: ISubjectService, IServiceHelper<CreateSubjectRequest>
|
||||
{
|
||||
private readonly SanStudentContext _context = context;
|
||||
private readonly IDistributedCache _cache = cache;
|
||||
private List<string> _cacheKeys = [];
|
||||
|
||||
public async Task<CreateSubjectResponse> CreateAsync(CreateSubjectRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
var problems = await Validate(request);
|
||||
if (problems.Errors.Count > 0)
|
||||
{
|
||||
return new CreateSubjectResponse
|
||||
{
|
||||
Ok = false,
|
||||
CreateSubjectResult = string.Join(", ", problems.Errors)
|
||||
};
|
||||
}
|
||||
|
||||
var subject = new Subject()
|
||||
{
|
||||
Name = request.Name!.Trim(),
|
||||
ShortName = request.ShortName!.Trim()
|
||||
};
|
||||
|
||||
_context.Subjects.Add(subject);
|
||||
await _context.SaveChangesAsync();
|
||||
await RemoveCache();
|
||||
return new CreateSubjectResponse
|
||||
{
|
||||
Ok = true,
|
||||
CreateSubjectResult = $"Zapisano nowy przedmiot \"{subject.Name}\"",
|
||||
};
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
return new CreateSubjectResponse
|
||||
{
|
||||
Ok = false,
|
||||
CreateSubjectResult = $"Błąd podczas próby zapisu przedmiotu: {error.Message}",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<SubjectDto[]> GetAllAsync()
|
||||
{
|
||||
var key = "subjects";
|
||||
AddKeyToCache(key);
|
||||
var cachedSubjects = await _cache.GetStringAsync(key);
|
||||
if (!string.IsNullOrEmpty(cachedSubjects))
|
||||
return cachedSubjects.FromJson<SubjectDto[]>();
|
||||
|
||||
var query = _context.Subjects as IQueryable<Subject>;
|
||||
var result = await query
|
||||
.OrderBy(s => s.Name)
|
||||
.ThenBy(s => s.ShortName)
|
||||
.Select(s => s.ToSubjectDto()).ToArrayAsync();
|
||||
|
||||
await _cache.SetWithExpirationTimeAsync(key, result, 262487);
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<PagedList<SubjectDto>> GetAllPagedAsync(GetSubjectsRequest request)
|
||||
{
|
||||
var key = $"subjects_{request.PageNumber}_{request.PageSize}";
|
||||
if (!string.IsNullOrWhiteSpace(request.Name))
|
||||
key += $"_{request.Name.ToLower()}";
|
||||
|
||||
AddKeyToCache(key);
|
||||
var cachedSubjects = await _cache.GetStringAsync(key);
|
||||
if (!string.IsNullOrEmpty(cachedSubjects))
|
||||
return cachedSubjects.FromJson<PagedList<SubjectDto>>();
|
||||
|
||||
var query = _context.Subjects as IQueryable<Subject>;
|
||||
|
||||
if (!string.IsNullOrEmpty(request.Name))
|
||||
query = query.Where(s => s.Name.ToLower().StartsWith(request.Name.ToLower()));
|
||||
|
||||
var data = query
|
||||
.OrderBy(s => s.Name)
|
||||
.ThenBy(s => s.ShortName)
|
||||
.Select(s => s.ToSubjectDto());
|
||||
|
||||
var result = await data.ToPagedListAsync(request.PageNumber, request.PageSize);
|
||||
await _cache.SetWithExpirationTimeAsync(key, result, 262487);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
public async Task<ValidationProblems> Validate(CreateSubjectRequest request)
|
||||
{
|
||||
var problems = new ValidationProblems();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(request.Name))
|
||||
problems.Errors.Add("Brak nazwy przedmiotu");
|
||||
|
||||
if (string.IsNullOrWhiteSpace(request.ShortName))
|
||||
problems.Errors.Add("Brak nazwy skróconej przedmiotu");
|
||||
|
||||
if (problems.Errors.Count > 0)
|
||||
return problems;
|
||||
|
||||
var subject = await _context.Subjects
|
||||
.FirstOrDefaultAsync(s => s.Name.ToLower() == request.Name!.ToLower());
|
||||
|
||||
if (subject is not null)
|
||||
problems.Errors.Add("Przedmiot o podanej nazwie już istnieje");
|
||||
|
||||
subject = await _context.Subjects
|
||||
.FirstOrDefaultAsync(s => s.ShortName.ToLower() == request.ShortName!.ToLower());
|
||||
|
||||
if (subject is not null)
|
||||
problems.Errors.Add("Przedmiot o podanej nazwie skróconej już istnieje");
|
||||
|
||||
return problems;
|
||||
}
|
||||
|
||||
public void AddKeyToCache(string key)
|
||||
{
|
||||
if (!_cacheKeys.Contains(key))
|
||||
_cacheKeys.Add(key);
|
||||
}
|
||||
|
||||
public async Task RemoveCache()
|
||||
{
|
||||
foreach (var cacheKey in _cacheKeys)
|
||||
await _cache.RemoveAsync(cacheKey);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user