Beispiel #1
0
        public async Task <IActionResult> Update(long id, TemplatePatchDTO templatePatchDTO)
        {
            await _bll.Templates.UpdateAsync(id, templatePatchDTO);

            return(NoContent());
        }
Beispiel #2
0
        public async Task UpdateAsync(long id, TemplatePatchDTO templatePatchDTO)
        {
            var template = await ValidateAndReturnTemplateAsync(id, templatePatchDTO.Id);

            template.Name = templatePatchDTO.Name;

            await UnitOfWork.Templates.UpdateAsync(template);

            var templateAttributesCount = await UnitOfWork.TemplateAttributes.CountByTemplateId(id);

            foreach (var attributePatchDTO in templatePatchDTO.Attributes.OrderBy(dto => dto.PatchOption))
            {
                TemplateAttribute attribute;

                switch (attributePatchDTO.PatchOption)
                {
                case PatchOption.Updated:
                    if (!(attributePatchDTO.Id.HasValue &&
                          await UnitOfWork.TemplateAttributes.AnyAsync(attributePatchDTO.Id.Value, id)))
                    {
                        throw new NotFoundException("Атрибут не найден");
                    }

                    await ValidateAndFormatAttribute(attributePatchDTO);

                    attribute =
                        await UnitOfWork.TemplateAttributes.FirstOrDefaultNoTrackAsync(attributePatchDTO.Id.Value) !;

                    attribute.Featured    = attributePatchDTO.Featured;
                    attribute.AttributeId = attributePatchDTO.AttributeId;

                    await UnitOfWork.TemplateAttributes.UpdateAsync(attribute);

                    break;

                case PatchOption.Created:

                    await ValidateAndFormatAttribute(attributePatchDTO);

                    attribute = new TemplateAttribute()
                    {
                        Featured    = attributePatchDTO.Featured,
                        AttributeId = attributePatchDTO.AttributeId,
                        TemplateId  = template.Id
                    };

                    await UnitOfWork.TemplateAttributes.AddAsync(attribute);

                    templateAttributesCount++;

                    break;

                case PatchOption.Deleted:
                    if (!(attributePatchDTO.Id.HasValue &&
                          await UnitOfWork.TemplateAttributes.AnyAsync(attributePatchDTO.Id.Value, id)))
                    {
                        throw new NotFoundException("Атрибут не найден");
                    }

                    await UnitOfWork.TemplateAttributes.RemoveAsync(attributePatchDTO.Id.Value);

                    templateAttributesCount--;

                    break;
                }
            }

            if (templateAttributesCount < 1)
            {
                throw new ValidationException("В шаблоне должен быть как минимум один атрибут");
            }

            await UnitOfWork.SaveChangesAsync();
        }