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, IGroupService { private readonly IStringLocalizer? _localizer; public GroupService(IGroupRepository repository, IMapper mapper, IStringLocalizer? localizer = null) : base(repository, mapper) { _localizer = localizer; } public async Task> CreateAsync(DirectoryGroupDto adGroup) { var group = _mapper.Map(adGroup); //set the user var user = await GetUserAsync(); group.AddedWho = user?.AddedWho ?? "UNAUTHORIZED"; if (await HasEntity(group.Id)) return Result.Fail().Message(_localizer?[Key.GroupAlreadyExists].Value); var createdGroup = await _repository.CreateAsync(group); if (createdGroup is null) return Result.Fail(); else return Result.Success(createdGroup.Id); } } }