public SectionFullDTO Update(int id, SectionFullDTO dto) { var section = Context.Sections .Include(s => s.ParentSection) .SingleOrDefault(s => s.Id == id); if (section == null) { throw new IdNotFoundRequestException(nameof(Section), id); } if (dto.ParentSectionId != null) { var parentSection = Context.Sections .SingleOrDefault(s => s.Id == dto.ParentSectionId); if (parentSection == null) { throw new IdNotFoundRequestException(nameof(Section), id); } } Context.Entry(section).OriginalValues["xmin"] = dto.RowVersion; section.Name = dto.Name; section.ParentSectionId = dto.ParentSectionId; section.Cry = dto.Cry; section.Ages = dto.Ages; section.Sex = SexTypesConverter.FromEnum(dto.Sex); Context.SaveChanges(); var updatedDTO = new SectionFullDTO().FromEntity(section); return(updatedDTO); }
public SectionFullDTO Create(SectionFullDTO dto) { var parentSection = Context.Sections.SingleOrDefault(s => s.Id == dto.ParentSectionId); if (parentSection == null) { throw new IdNotFoundRequestException(nameof(Section), dto.ParentSectionId.ToString()); } var newSection = new Section(dto.Name, dto.Cry, SexTypesConverter.FromEnum(dto.Sex), dto.Ages, null); newSection.ParentSectionId = dto.ParentSectionId; Context.Add(newSection); Context.SaveChanges(); return(new SectionFullDTO().FromEntity(newSection)); }