You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
2.3 KiB
85 lines
2.3 KiB
using AppContext.Entities;
|
|
using Converters;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Model;
|
|
using Services;
|
|
|
|
namespace DbServices;
|
|
|
|
public class DbTeamService(AppContext.AppContext context) : ITeamService
|
|
{
|
|
public Task<IEnumerable<Team>> ListTeamsOf(int userId)
|
|
{
|
|
return Task.FromResult(
|
|
context.Teams
|
|
.Include(t => t.Members)
|
|
.Where(t => t.Members.Any(m => m.UserId == userId))
|
|
.AsEnumerable()
|
|
.Select(t => t.ToModel())
|
|
);
|
|
}
|
|
|
|
public Task<IEnumerable<Team>> ListTeams(string nameNeedle)
|
|
{
|
|
return Task.FromResult(
|
|
context.Teams.Where(t => t.Name.ToLower().Contains(nameNeedle.ToLower()))
|
|
.AsEnumerable()
|
|
.Select(e => e.ToModel())
|
|
);
|
|
}
|
|
|
|
public Task<IEnumerable<Team>> ListTeams()
|
|
{
|
|
return Task.FromResult(
|
|
context.Teams
|
|
.AsEnumerable()
|
|
.Select(e => e.ToModel())
|
|
);
|
|
}
|
|
|
|
public async Task<int> CountTeams(string nameNeedle)
|
|
{
|
|
return await context.Teams.CountAsync(t => t.Name.ToLower().Contains(nameNeedle.ToLower()));
|
|
}
|
|
|
|
public async Task<int> CountTeams()
|
|
{
|
|
return await context.Teams.CountAsync();
|
|
}
|
|
|
|
public async Task<Team> 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<bool> 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;
|
|
}
|
|
} |