public RegionModel UpdateRegion(RegionModel model) { if (model == null) throw new ArgumentNullException("model"); Region RegionInDb; using (var unitOfWork = new UnitOfWork(_connectionString)) { bool userAuthorised = UserAuthorizedToAccessSuite(unitOfWork, model.UserId, model.SuiteId, new[] {RoleType.Admin}); if (!userAuthorised) throw new UnauthorizedUserException( "User does not have access or sufficient privileges for this action to suite: " + model.SuiteId); RegionInDb = unitOfWork.Context.Regions.FirstOrDefault( x => x.RegionId == model.RegionId && x.SuiteId == model.SuiteId); if (RegionInDb != null) { RegionInDb.Description = model.Description; RegionInDb.RegionName = model.RegionName; unitOfWork.Update(RegionInDb); } else { throw new InvalidOperationException("No Region exists with id:" + model.RegionId + " and suite id: " + model.SuiteId); } } return RegionInDb.ToModel(); }
public RegionModel AddRegion(RegionModel model) { if (model == null) throw new ArgumentNullException("model"); Region appToReturn; using (var unitOfWork = new UnitOfWork(_connectionString)) { bool userAuthorised = UserAuthorizedToAccessSuite(unitOfWork, model.UserId, model.SuiteId, new[] {RoleType.Admin}); if (!userAuthorised) throw new UnauthorizedUserException( "User does not have access or sufficient privileges for this action to suite: " + model.SuiteId); appToReturn = model.ToNewDbObject(); unitOfWork.Add(appToReturn); } if (appToReturn != null) return appToReturn.ToModel(); return null; }