Example #1
0
        public ActionResult UpdateContest(UpdateContestBindingModel model)
        {
            if (model == null)
            {
                this.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(this.Json(new { ErrorMessage = "Missing Data" }));
            }

            if (!this.ModelState.IsValid)
            {
                this.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(this.Json(this.ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage)));
            }

            try
            {
                this._service.UpdateContest(model, this.User.Identity.GetUserId());

                return(this.RedirectToAction("ManageContest", new { id = model.Id }));
            }
            catch (NotFoundException exception)
            {
                return(this.HttpNotFound(exception.Message));
            }
            catch (UnauthorizedException exception)
            {
                this.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                return(this.Json(new { ErrorMessage = exception.Message }));
            }
        }
        public void UpdateExistingContestFromNonOrganizatorShouldReturnErrorMessage()
        {
            var contest = new UpdateContestBindingModel {
                Id = 1, Title = "New title"
            };

            var result = this.fakeContestsController.UpdateContest(contest);

            Assert.IsInstanceOfType(result, typeof(JsonResult));
        }
        public void UpdateContestWithInvalidIdShouldReturnHttpNotFound()
        {
            var contest = new UpdateContestBindingModel {
                Id = 100, Title = "New title"
            };

            var result = this.fakeContestsController.UpdateContest(contest);

            Assert.IsInstanceOfType(result, typeof(HttpNotFoundResult));
        }
Example #4
0
        public bool UpdateContest(UpdateContestBindingModel model, string userId)
        {
            var contest = this.Data.Contests.Find(model.Id);

            if (contest == null)
            {
                throw new NotFoundException("The selected contest does not exist");
            }

            var loggedUserId = userId;

            if (contest.OrganizatorId != loggedUserId)
            {
                throw new UnauthorizedException("Logged user is not the contest organizator");
            }

            if (!string.IsNullOrWhiteSpace(model.Title))
            {
                contest.Title = model.Title;
            }

            if (!string.IsNullOrWhiteSpace(model.Description))
            {
                contest.Description = model.Description;
            }

            if (model.EndDate != default(DateTime))
            {
                contest.EndDate = model.EndDate;
            }

            this.Data.Contests.Update(contest);
            this.Data.SaveChanges();

            return(true);
        }
        public bool UpdateContest(UpdateContestBindingModel model, string userId)
        {
            var contest = this.Data.Contests.Find(model.Id);

            if (contest == null)
            {
                throw new NotFoundException("The selected contest does not exist");
            }

            var loggedUserId = userId;

            if (contest.OrganizatorId != loggedUserId)
            {
                throw new UnauthorizedException("Logged user is not the contest organizator");
            }

            if (!string.IsNullOrWhiteSpace(model.Title))
            {
                contest.Title = model.Title;
            }

            if (!string.IsNullOrWhiteSpace(model.Description))
            {
                contest.Description = model.Description;
            }

            if (model.EndDate != default(DateTime))
            {
                contest.EndDate = model.EndDate;
            }

            this.Data.Contests.Update(contest);
            this.Data.SaveChanges();

            return true;
        }
        public void UpdateExistingContestFromNonOrganizatorShouldReturnErrorMessage()
        {
            var contest = new UpdateContestBindingModel {Id = 1, Title = "New title"};

            var result = this.fakeContestsController.UpdateContest(contest);

            Assert.IsInstanceOfType(result, typeof(JsonResult));
        }
        public void UpdateContestWithInvalidIdShouldReturnHttpNotFound()
        {
            var contest = new UpdateContestBindingModel {Id = 100, Title = "New title"};

            var result = this.fakeContestsController.UpdateContest(contest);

            Assert.IsInstanceOfType(result, typeof(HttpNotFoundResult));
        }
        public ActionResult UpdateContest(UpdateContestBindingModel model)
        {
            if (model == null)
            {
                this.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return this.Json(new { ErrorMessage = "Missing Data" } );
            }

            if (!this.ModelState.IsValid)
            {
                this.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return this.Json(this.ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage));
            }

            try
            {
                this._service.UpdateContest(model, this.User.Identity.GetUserId());

                return this.RedirectToAction("ManageContest", new { id = model.Id });
            }
            catch (NotFoundException exception)
            {
                return this.HttpNotFound(exception.Message);
            }
            catch (UnauthorizedException exception)
            {
                this.Response.StatusCode = (int) HttpStatusCode.Unauthorized;
                return this.Json(new { ErrorMessage = exception.Message });
            }
        }