2024-06-13 14:16:39 +02:00

34 lines
1.3 KiB
C#

using AutoMapper;
using DigitalData.Core.Application;
using DigitalData.Core.DTO;
using DigitalData.UserManager.Application.Contracts;
using DigitalData.UserManager.Application.DTOs.Group;
using DigitalData.UserManager.Domain.Entities;
using DigitalData.UserManager.Infrastructure.Contracts;
using Microsoft.Extensions.Localization;
namespace DigitalData.UserManager.Application.Services
{
public class GroupService : CRUDService<IGroupRepository, GroupCreateDto, GroupReadDto, GroupUpdateDto, Group, int>, IGroupService
{
private readonly IStringLocalizer<Resource> _localizer;
public GroupService(IGroupRepository repository, IStringLocalizer<Resource> localizer, IMapper mapper) : base(repository, mapper)
{
_localizer = localizer;
}
public async Task<DataResult<int>> CreateAsync(DirectoryGroupDto adGroup)
{
var group = _mapper.MapOrThrow<Group>(adGroup);
if (await HasEntity(group.Guid))
return Result.Fail<int>().Message(_localizer[Key.GroupAlreadyExists.ToString()]);
var createdGroup = await _repository.CreateAsync(group);
if (createdGroup is null)
return Result.Fail<int>();
else
return Result.Success(KeyValueOf(createdGroup));
}
}
}