diff --git a/API/Controllers/Admin/TeamsController.cs b/API/Controllers/Admin/TeamsController.cs new file mode 100644 index 0000000..6418fe3 --- /dev/null +++ b/API/Controllers/Admin/TeamsController.cs @@ -0,0 +1,67 @@ +using System.ComponentModel.DataAnnotations; +using Microsoft.AspNetCore.Mvc; +using Model; +using Services; + +namespace API.Controllers; + +[ApiController] +public class TeamsController(ITeamService service) : ControllerBase +{ + public record CountTeamsResponse(int Value); + + + [HttpGet("/admin/teams/count")] + public async Task CountTeams() + { + return new CountTeamsResponse(await service.CountTeams()); + } + + // [HttpGet("/admin/users/count")] + // public async Task CountUsers() + // { + // return new CountUsersResponse(await service.UsersCount()); + // } + + [HttpGet("/admin/teams")] + public async Task> ListTeams( + [Range(0, int.MaxValue, ErrorMessage = "Only positive number allowed")] + int start, + [Range(0, int.MaxValue, ErrorMessage = "Only positive number allowed")] + int n + ) + { + var result = await service.ListTeams(); + + return result.Skip(start).Take(n); + } + + public record AddTeamRequest(string Name, string Picture, string FirstColor, string SecondColor); + + [HttpPost("/admin/teams")] + public async Task AddTeam([FromBody] AddTeamRequest req) + { + var team = await service.AddTeam(req.Name, req.Picture, req.FirstColor, req.SecondColor); + + return Ok(team); + } + + public record UpdateTeamRequest(int Id, string Name, string Picture, string MainColor, string SecondaryColor); + + [HttpPut("/admin/teams/{teamId:int}")] + public async Task UpdateTeam([FromBody] UpdateTeamRequest req) + { + await service.UpdateTeam(new Team(req.Id, req.Name, req.Picture, req.MainColor, req.SecondaryColor)); + + return Ok(); + } + + public record DeleteTeamsRequest(int[] Teams); + + [HttpPost("/admin/teams/remove-all")] + public async Task DeleteTeams([FromBody] DeleteTeamsRequest req) + { + await service.RemoveTeams(req.Teams); + return Ok(); + } +} \ No newline at end of file diff --git a/API/Controllers/AccountsController.cs b/API/Controllers/Admin/UsersController.cs similarity index 97% rename from API/Controllers/AccountsController.cs rename to API/Controllers/Admin/UsersController.cs index 60db85c..f93f6af 100644 --- a/API/Controllers/AccountsController.cs +++ b/API/Controllers/Admin/UsersController.cs @@ -6,7 +6,7 @@ using Services; namespace API.Controllers; [ApiController] -public class AccountsController(IUserService service) : ControllerBase +public class UsersController(IUserService service) : ControllerBase { private const string DefaultProfilePicture = "https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_960_720.png"; diff --git a/AppContext/Entities/TeamEntity.cs b/AppContext/Entities/TeamEntity.cs index f3b8b3f..055b3d8 100644 --- a/AppContext/Entities/TeamEntity.cs +++ b/AppContext/Entities/TeamEntity.cs @@ -3,7 +3,7 @@ namespace AppContext.Entities; public class TeamEntity { public int Id { get; set; } - public required string Name { get; init; } + public required string Name { get; set; } public required string Picture { get; set; } public required string MainColor { get; set; } public required string SecondColor { get; set; } diff --git a/DbServices/DbTeamService.cs b/DbServices/DbTeamService.cs index af22160..0393b84 100644 --- a/DbServices/DbTeamService.cs +++ b/DbServices/DbTeamService.cs @@ -1,3 +1,4 @@ +using AppContext.Entities; using Converters; using Microsoft.EntityFrameworkCore; using Model; @@ -17,4 +18,68 @@ public class DbTeamService(AppContext.AppContext context) : ITeamService .Select(t => t.ToModel()) ); } + + public Task> ListTeams(string nameNeedle) + { + return Task.FromResult( + context.Teams.Where(t => t.Name.ToLower().Contains(nameNeedle.ToLower())) + .AsEnumerable() + .Select(e => e.ToModel()) + ); + } + + public Task> ListTeams() + { + return Task.FromResult( + context.Teams + .AsEnumerable() + .Select(e => e.ToModel()) + ); + } + + public async Task CountTeams(string nameNeedle) + { + return await context.Teams.CountAsync(t => t.Name.ToLower().Contains(nameNeedle.ToLower())); + } + + public async Task CountTeams() + { + return await context.Teams.CountAsync(); + } + + public async Task AddTeam(string name, string picture, string firstColor, string secondColor) + { + var entity = new TeamEntity + { + Name = name, + Picture = picture, + MainColor = firstColor, + SecondColor = secondColor + }; + + await context.Teams.AddAsync(entity); + await context.SaveChangesAsync(); + return entity.ToModel(); + } + + public async Task RemoveTeams(params int[] teams) + { + await context.Teams + .Where(t => teams.Contains(t.Id)) + .ExecuteDeleteAsync(); + } + + public async Task UpdateTeam(Team team) + { + var entity = await context.Teams.FirstOrDefaultAsync(t => t.Id == team.Id); + if (entity == null) + return false; + + entity.Name = team.Name; + entity.MainColor = team.MainColor; + entity.SecondColor = team.SecondColor; + entity.Picture = team.Picture; + + return await context.SaveChangesAsync() > 0; + } } \ No newline at end of file diff --git a/Services/ITeamService.cs b/Services/ITeamService.cs index 81e31bc..ae19f90 100644 --- a/Services/ITeamService.cs +++ b/Services/ITeamService.cs @@ -4,7 +4,14 @@ namespace Services; public interface ITeamService { - public Task> ListTeamsOf(int userId); - + + + public Task> ListTeams(); + public Task CountTeams(); + + public Task AddTeam(string name, string picture, string firstColor, string secondColor); + public Task RemoveTeams(params int[] teams); + + public Task UpdateTeam(Team team); } \ No newline at end of file