public void Init() { _fakeRestrictionRepository = new Mock <IRestrictionRepository>(); _fakeSalesAreaRepository = new Mock <ISalesAreaRepository>(); _fakeClearanceRepository = new Mock <IClearanceRepository>(); _fakeProgrammeCategoryRepository = new Mock <IProgrammeCategoryHierarchyRepository>(); var salesAreaNames = new List <string> { "sa1" }; var salesArea = new SalesArea { Name = "sa1" }; var programmeCategory = new ProgrammeCategoryHierarchy { Id = 2, Name = "CHILDREN" }; _ = _fakeSalesAreaRepository.Setup(m => m.FindByNames(salesAreaNames)).Returns( new List <SalesArea> { salesArea } ); _ = _fakeProgrammeCategoryRepository.Setup(p => p.GetAll()).Returns( new List <ProgrammeCategoryHierarchy> { programmeCategory }); _fixture = new Fixture(); _mapper = AutoMapperInitializer.Initialize(Configuration.AutoMapperConfig.SetupAutoMapper); _controller = new RestrictionController( _fakeRestrictionRepository.Object, _fakeSalesAreaRepository.Object, _fakeClearanceRepository.Object, _fakeProgrammeCategoryRepository.Object, _mapper); _validCreateRestriction = CreateValidModel(); _validUpdateRestriction = UpdateValidModel(); _validCreateRestrictions = new List <CreateRestriction>(); }
public IHttpActionResult Put([FromUri] string externalIdentifier, [FromBody] UpdateRestrictionModel command) { if (!ModelState.IsValid) { return(this.Error().InvalidParameters()); } if (string.IsNullOrWhiteSpace(externalIdentifier)) { return(this.Error().BadRequest("External identifier is required for this request")); } var restriction = _restrictionRepository.Get(externalIdentifier); if (restriction == null) { ValidateInput(_mapper.Map <CreateRestriction>(command)); restriction = _mapper.Map <Restriction>(command); restriction.Uid = Guid.NewGuid(); EnsureThatAllSalesAreasAdded(restriction); restriction.ExternalIdentifier = externalIdentifier; } else { ValidateRestrictionDatesAndDays(command.StartDate, command.EndDate, command.RestrictionDays); UpdateRestriction(ref restriction, command); } _restrictionRepository.Add(restriction); _restrictionRepository.SaveChanges(); return(Ok(_mapper.Map <RestrictionModel>(restriction))); }