public async Task <ServiceResponse <AttachmentTypeDto> > UpdateAsync(AttachmentTypeDto attachmentType, int userId) { if (attachmentType == null) { throw new ArgumentNullException(nameof(attachmentType)); } var existentEntity = await UnitOfWork.Get <AttachmentType>().GetAsync(t => t.ID == attachmentType.ID); if (existentEntity == null) { return(new ServiceResponse <AttachmentTypeDto>(ServiceResponseStatus.NotFound)); } var attachmentTypes = UnitOfWork.Get <AttachmentType>().GetAll(t => t.Name.Equals(attachmentType.Name) && t.ID != attachmentType.ID); if (attachmentTypes.Any()) { return(new ServiceResponse <AttachmentTypeDto>(ServiceResponseStatus.AlreadyExists)); } var entity = attachmentType.ToEntity(); existentEntity.UpdateFields(entity); existentEntity.UpdateModifiedFields(userId); UnitOfWork.Get <AttachmentType>().Update(existentEntity); await UnitOfWork.SaveAsync(); return(new SuccessResponse <AttachmentTypeDto>()); }
public static AttachmentType ToEntity(this AttachmentTypeDto dto) { return(new AttachmentType { ID = dto.ID, Name = dto.Name, Comment = dto.Comment, Extensions = Regex.Replace(dto.Extensions.ToLower(), @"\s+", "") }); }
public static AttachmentTypeDto ToDto(this AttachmentType model) { var dto = new AttachmentTypeDto { ID = model.ID, Name = model.Name, Comment = model.Comment, Extensions = model.Extensions, AttachmentsCount = model.TypeAttachments?.Count ?? 0, CanBeUpdated = !model.Name.Equals("Основное изображение") }; dto.MapDetails(model); dto.CanBeDeleted = (model.Attachments?.All(a => a.IsDeleted) ?? false) && !model.IsDeleted && !model.Name.Equals("Основное изображение"); return(dto); }