public ConcretePartModel RegisterConcretePart(AddConcretePartDto dto) { return(ProtectedExecute <AddConcretePartDto, ConcretePartModel>(partDto => { AdminService.CheckActiveAdmin(partDto.AdminSession); PartModel part = PartRepo.Get(partDto.PartId.GetValueOrDefault()); if (part == null) { throw new NotFoundException("part"); } MaterialModel material = part.PossibleMaterials.FirstOrDefault(mat => mat.Id == partDto.MaterialId); if (material == null) { throw new NotFoundException("possible material"); } PartColorModel color = material.PossibleColors.FirstOrDefault(col => col.Id == partDto.ColorId); if (color == null) { throw new NotFoundException("possible color"); } if (partDto.ControllerMac == null && partDto.Amount == null) { throw new NotFoundException("nor controller_mac neither amount"); } if (partDto.ControllerMac == null && part.ConnectionHelpers.Count() != 0) { throw new NotFoundException("controller mac for assigned connection helpers"); } if (partDto.ControllerMac != null && part.ConnectionHelpers.Count() == 0) { throw new NotFoundException("connection helpers to embed a controller"); } if (partDto.ControllerMac != null && ConcretePartRepo.GetPartByMac(partDto.ControllerMac) != null) { throw new ConflictException("mac address"); } if (partDto.ControllerMac == null) { ICollection <ConcretePartModel> created = new List <ConcretePartModel>(); ConcretePartModel model = Mapper.Map <AddConcretePartDto, ConcretePartModel>(partDto); for (int i = 0; i < partDto.Amount.GetValueOrDefault(); ++i) { created.Add(ConcretePartRepo.Create(model)); } return created.LastOrDefault(); } return ConcretePartRepo.Create(Mapper.Map <AddConcretePartDto, ConcretePartModel>(partDto)); }, dto)); }
public PartColorModel RegisterColor(AddColorDto dto) { return(ProtectedExecute <AddColorDto, PartColorModel>(colorDto => { AdminService.CheckActiveSuperAdmin(colorDto.SuperAdminSession); PartColorModel model = Mapper.Map <AddColorDto, PartColorModel>(colorDto); if (ColorRepo.GetByName(model.Name) != null) { throw new ConflictException("Color name"); } return ColorRepo.Create(model); }, dto)); }
public PartColorModel UpdateColor(UpdateColorDto dto) { return(ProtectedExecute <UpdateColorDto, PartColorModel>(colorDto => { AdminService.CheckActiveSuperAdmin(colorDto.SuperAdminSession); PartColorModel model = Mapper.Map <UpdateColorDto, PartColorModel>(colorDto); PartColorModel foundColor = ColorRepo.GetByName(model.Name); if (foundColor != null && foundColor.Id != model.Id) { throw new ConflictException("Color name"); } return ColorRepo.Update(model.Id, model); }, dto)); }