public ServerModel UpdateServer(ServerModel model) { if (model == null) throw new ArgumentNullException("model"); Server ServerInDb; 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); ServerInDb = unitOfWork.Context.Servers.FirstOrDefault( x => x.ServerId == model.ServerId && x.SuiteId == model.SuiteId); if (ServerInDb != null) { ServerInDb.Description = model.Description; ServerInDb.ServerName = model.ServerName; unitOfWork.Update(ServerInDb); } else { throw new InvalidOperationException("No Server exists with id:" + model.ServerId + " and suite id: " + model.SuiteId); } } return ServerInDb.ToModel(); }
public ServerModel AddServer(ServerModel model) { if (model == null) throw new ArgumentNullException("model"); Server 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; }