public void DeclineRequest(RequestModel notification, UsersModel user) { var db = new angelhackEntities(); UsersModel userRepo = new UsersModel(); using (var transaction = db.Database.BeginTransaction()) { try { userRepo.CheckUserIfExist(db, user); userRepo.IsUserServiceProvider(db, user); var newNotification = db.requestnotifications.Where(x => x.UserID == user.ID && x.IsDeclined == false && ( x.request.StatusID != (int)RequestStatus.CANCELLED && x.request.StatusID != (int)RequestStatus.SERVED && x.request.StatusID != (int)RequestStatus.CLOSED ) && x.RequestID == notification.ID).FirstOrDefault(); if (newNotification == null) { throw new Exception("Request no longer exist!"); } newNotification.DateLastUpdated = DateTime.Now; newNotification.IsDeclined = true; db.SaveChanges(); transaction.Commit(); } catch (System.Data.Entity.Infrastructure.DbUpdateException dbU) { transaction.Rollback(); Exception ex = dbU.GetBaseException(); throw new Exception(ex.Message); } catch (DbEntityValidationException dbEx) { transaction.Rollback(); string errorMessages = null; foreach (DbEntityValidationResult validationResult in dbEx.EntityValidationErrors) { string entityName = validationResult.Entry.Entity.GetType().Name; foreach (DbValidationError error in validationResult.ValidationErrors) { errorMessages += (entityName + "." + error.PropertyName + ": " + error.ErrorMessage); } } throw new Exception(errorMessages); } finally { db.Database.Connection.Close(); } } }
public RequestModel Create(RequestModel request, UsersModel requestor) { var db = new angelhackEntities(); using (var transaction = db.Database.BeginTransaction()) { try { var chkUser = db.users.Where(x => x.ID == requestor.ID).FirstOrDefault(); if (chkUser == null) { throw new Exception("User does not exist!"); } var newRequest = new request(); newRequest.CurrentLocation = request.CurrentLocation; newRequest.DateLastUpdated = DateTime.Now; newRequest.DateRequested = DateTime.Now; newRequest.DeliveryDate = request.DeliveryDate; newRequest.Description = request.Description; newRequest.StatusID = (int)request.StatusID; newRequest.UserID = requestor.ID; db.requests.Add(newRequest); db.SaveChanges(); transaction.Commit(); request.ID = newRequest.ID; request.UserID = requestor.ID; request.User = mapper.Map <user, UsersModel>(chkUser); return(request); } catch (System.Data.Entity.Infrastructure.DbUpdateException dbU) { transaction.Rollback(); Exception ex = dbU.GetBaseException(); throw new Exception(ex.Message); } catch (DbEntityValidationException dbEx) { transaction.Rollback(); string errorMessages = null; foreach (DbEntityValidationResult validationResult in dbEx.EntityValidationErrors) { string entityName = validationResult.Entry.Entity.GetType().Name; foreach (DbValidationError error in validationResult.ValidationErrors) { errorMessages += (entityName + "." + error.PropertyName + ": " + error.ErrorMessage); } } throw new Exception(errorMessages); } finally { db.Database.Connection.Close(); } } }
public void CancelRequest(RequestModel request, UsersModel requestor) { var db = new angelhackEntities(); using (var transaction = db.Database.BeginTransaction()) { try { var chkUser = db.users.Where(x => x.ID == requestor.ID).FirstOrDefault(); if (chkUser == null) { throw new Exception("User does not exist!"); } var newRequest = db.requests.Where(x => x.ID == request.ID && ( x.StatusID != (int)RequestStatus.CANCELLED && x.StatusID != (int)RequestStatus.SERVED && x.StatusID != (int)RequestStatus.CLOSED ) && x.UserID == requestor.ID).FirstOrDefault(); if (newRequest == null) { throw new Exception("Request does not exist!"); } newRequest.StatusID = (int)RequestStatus.CANCELLED; db.SaveChanges(); transaction.Commit(); } catch (System.Data.Entity.Infrastructure.DbUpdateException dbU) { transaction.Rollback(); Exception ex = dbU.GetBaseException(); throw new Exception(ex.Message); } catch (DbEntityValidationException dbEx) { transaction.Rollback(); string errorMessages = null; foreach (DbEntityValidationResult validationResult in dbEx.EntityValidationErrors) { string entityName = validationResult.Entry.Entity.GetType().Name; foreach (DbValidationError error in validationResult.ValidationErrors) { errorMessages += (entityName + "." + error.PropertyName + ": " + error.ErrorMessage); } } throw new Exception(errorMessages); } finally { db.Database.Connection.Close(); } } }