Пример #1
0
        // GET: Feature/Create
        public ActionResult Create(int?id)
        {
            //Check request was valid
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, "No Project Id presented."));
            }

            //Ensure project exists
            var project = _db.Projects.Find(id);

            if (project == null)
            {
                return(HttpNotFound("Project not found."));
            }

            //TODO: Ensure user is allowed access to this project

            //Create view model
            var model = new FeatureCreateViewModel()
            {
                ProjectId = project.Id
            };

            return(View("_Create", model));
        }
Пример #2
0
        public async Task <ActionResult> Create(FeatureCreateViewModel model)
        {
            //Check if model is valid
            if (!ModelState.IsValid)
            {
                return(PartialView("_Create", model));
            }

            //Find project in database
            var project = _db.Projects.Find(model.ProjectId);

            if (project == null)
            {
                return(HttpNotFound("Project not found"));
            }

            //TODO: Ensure user is allowed access to this project

            //Create feature from model
            var feature = new Feature()
            {
                Project     = project,
                Title       = model.Title,
                Description = model.Description,
                Weight      = model.Weight,
                Priority    = project.Features.Any() ? project.Features.OrderByDescending(p => p.Priority).First().Priority + 1 : 1,
                DateCreated = DateTime.Now
            };

            //Save feature in database
            try
            {
                _db.Features.Add(feature);
                await _db.SaveChangesAsync();

                return(Json(new { success = true }));
            }
            catch (Exception exception)
            {
                model.Error = exception.Message;
                return(PartialView("_Create", model));
            }
        }