37 lines
1.4 KiB
C#

using AutoMapper;
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 : BaseService<IGroupRepository, GroupCreateDto, GroupReadDto, Group>, 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.Map<Group>(adGroup);
//set the user
var user = await GetUserAsync();
group.AddedWho = user?.AddedWho ?? "UNAUTHORIZED";
if (await HasEntity(group.Id))
return Result.Fail<int>().Message(_localizer?[Key.GroupAlreadyExists].Value);
var createdGroup = await _repository.CreateAsync(group);
if (createdGroup is null)
return Result.Fail<int>();
else
return Result.Success(createdGroup.Id);
}
}
}