예제 #1
0
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            if (bindingContext == null)
            {
                throw new ArgumentNullException("Binding context is null", "bindingContext");
            }

            HttpRequestBase        formRequest = controllerContext.HttpContext.Request;
            RequestReviewViewModel result      = new RequestReviewViewModel();
            Guid requestId;
            DatabaseRequestType requestType;
            NodeContentType     nodeDataType;
            bool approved;
            BasicNodeViewModel nodeViewModel = null;

            if (!Guid.TryParse(formRequest.Form["RequestId"], out requestId))
            {
                bindingContext.ModelState.AddModelError("RequestId", "Invalid request id");
            }
            if (!Enum.TryParse <DatabaseRequestType>(formRequest.Form["RequestType"], out requestType))
            {
                bindingContext.ModelState.AddModelError("RequestType", "Invalid request type");
            }
            if (!Enum.TryParse <NodeContentType>(formRequest.Form["NodeDataType"], out nodeDataType))
            {
                bindingContext.ModelState.AddModelError("NodeDataType", "Invalid node data type");
            }
            // I don't really care about the full data for the deletion requests
            if (requestType != DatabaseRequestType.Delete)
            {
                nodeViewModel = (new NodeModelBinder()).BindModel(controllerContext, bindingContext) as BasicNodeViewModel;
            }
            else
            {
                nodeViewModel = new BasicNodeViewModel {
                    Id = Guid.Parse(formRequest.Form["Id"]), ContentType = nodeDataType
                };
            }
            if (!bool.TryParse(formRequest.Form["Approved"], out approved))
            {
                bindingContext.ModelState.AddModelError("Approved", "Invalid approval state");
            }

            if (bindingContext.ModelState.IsValid)
            {
                result = new RequestReviewViewModel
                {
                    RequestId    = requestId,
                    RequestType  = requestType,
                    NodeDataType = nodeDataType,
                    NodeData     = nodeViewModel,
                    Notes        = formRequest.Form["Notes"],
                    Approved     = approved
                };
            }

            return(result);
        }
예제 #2
0
        public ActionResult ViewRequest(RequestReviewViewModel request)
        {
            ActionResult result = View("Error");

            if (request != null)
            {
                DatabaseRequest fromDatabase;
                // Get the data from the request database
                using (ApplicationDbContext context = ApplicationDbContext.Create())
                {
                    fromDatabase = context.Requests.SingleOrDefault(x => x.Id == request.RequestId);
                    if (!fromDatabase.Reviewed)
                    {
                        // Only save changes to the database if the request was rejected or if the request was
                        // approved and the database was updated
                        bool saveDatabase = !request.Approved || (request.Approved && CheckRequestsAndCommitChanges(request.NodeData, fromDatabase));
                        // If we should delete requests when they are reviewed,
                        if (kDeleteOnReview)
                        {
                            // Delete the request
                            context.Requests.Remove(fromDatabase);
                        }
                        else
                        {
                            // Update the information in the database
                            fromDatabase.Reviewed     = true;
                            fromDatabase.ReviewedDate = DateTime.Now;
                            fromDatabase.Approved     = request.Approved;
                            fromDatabase.Notes        = request.Notes;
                            if (request.Approved)
                            {
                                fromDatabase.ApprovalDate = DateTime.Now;
                            }
                        }
                        // If the database action was successful,
                        if (saveDatabase)
                        {
                            // Save the changes to the database
                            context.SaveChanges();
                        }
                    }
                }

                result = RedirectToAction("DatabaseRequests");
            }

            return(result);
        }