コード例 #1
0
 public IActionResult Create(Work newWork)
 {
     try
     {
         if (ModelState.IsValid)
         {
             _work.Add(newWork);
             return(RedirectToAction("Index"));
         }
         return(View(newWork));
     }
     catch (Exception err)
     {
         ModelState.AddModelError(err.ToString(), "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
     }
     return(View(newWork));
 }
コード例 #2
0
        public async Task <IActionResult> CreateCountry([FromBody] Country country)
        {
            if (country == null)
            {
                throw new ArgumentNullException(nameof(country));
            }

            if (await _repo.CountryExistByName(country.Name))
            {
                ModelState.AddModelError("", $"Country {country.Name} already exists!");
                return(StatusCode(422, $"Country {country.Name} already exists!"));
            }


            if (!await _work.Add(country))
            {
                ModelState.AddModelError("", $"Something went wrong saving {country.Name}!!");
                return(StatusCode(500, ModelState));
            }

            return(CreatedAtRoute("GetCountry", new { countryId = country.Id }, country));
        }
コード例 #3
0
        public async Task <IActionResult> CreateReview([FromBody] Review review)
        {
            if (review == null)
            {
                throw new ArgumentNullException(nameof(review));
            }


            if (!await _reviewerRepo.ReviewerExists(review.Reviewer.Id))
            {
                ModelState.AddModelError("", "Reviewer doesn't exist!");
            }

            if (!await _bookRepo.BookExistsById(review.Book.Id))
            {
                ModelState.AddModelError("", "Book doesn't exist!");
            }

            if (!ModelState.IsValid)
            {
                return(StatusCode(404, ModelState));
            }


            review.Book = await _bookRepo.GetBookById(review.Book.Id);

            review.Reviewer = await _reviewerRepo.GetReviewer(review.Reviewer.Id);


            if (!await _work.Add(review))
            {
                ModelState.AddModelError("", $"Something went wrong saving the review!!");
                return(StatusCode(500, ModelState));
            }

            return(CreatedAtRoute("GetReview", new { reviewId = review.Id }, review));
        }