Esempio n. 1
0
        public IActionResult CreateCountry([FromBody] CountryDto countryDto)
        {
            if (countryDto == null)
            {
                return(BadRequest());
            }
            if (countryRepo.CountryExists(countryDto.CountryName))
            {
                ModelState.AddModelError("", "Naziv drzave vec postoji!");
                return(StatusCode(404, ModelState));
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var countryObj = mapper.Map <Country>(countryDto);

            if (!countryRepo.CreateCountry(countryObj))
            {
                ModelState.AddModelError("", $"Doslo je do greske u spasavanju {countryObj.CountryName}");
                return(StatusCode(500, ModelState));
            }
            return(CreatedAtRoute("GetCountry", new { id = countryObj.CountryId }, countryObj));
        }
        public IActionResult CreateDirector([FromBody] Director createDirector)
        {
            if (createDirector == null || !ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            //As Director entity model also has reference to Moview & DIrector object due to entity relationship, need to populate these both objects

            if (!countryRepository.CountryExists(createDirector.Country.Id)) //Id comes from View as hidden input when user selects Country from e.g. dropdown
            {
                ModelState.AddModelError("", "Country doesn't exists");
            }
            if (!ModelState.IsValid)
            {
                return(StatusCode(404, ModelState));
            }
            createDirector.Country = countryRepository.GetCountry(createDirector.Country.Id); //Id comes from View as hidden input when user selects Critic from e.g. dropdown

            if (!directorRepository.CreateDirector(createDirector))
            {
                ModelState.AddModelError("", "Something went wrong, Please try again");
                return(StatusCode(500, ModelState));
            }
            return(CreatedAtRoute("GetDirector", new { directorID = createDirector.Id }, createDirector));
        }
Esempio n. 3
0
        public IActionResult CreateAuthor([FromBody] Author authorToCreate)
        {
            if (authorToCreate == null)
            {
                return(BadRequest(ModelState));
            }

            if (!_countryRepository.CountryExists(authorToCreate.Country.Id))
            {
                ModelState.AddModelError("", "Country doesn't exist!");
                return(StatusCode(404, ModelState));
            }

            authorToCreate.Country = _countryRepository.GetCountry(authorToCreate.Country.Id);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!_authorRepository.CreateAuthor(authorToCreate))
            {
                ModelState.AddModelError("", $"Something went wrong saving the author " +
                                         $"{authorToCreate.FirstName} {authorToCreate.LastName}");
                return(StatusCode(500, ModelState));
            }

            return(CreatedAtRoute("GetAuthor", new { authorId = authorToCreate.Id }, authorToCreate));
        }
Esempio n. 4
0
        public IActionResult CreateAuthor([FromBody] Author authortoCreate)
        {
            if (authortoCreate == null)
            {
                return(BadRequest());
            }

            if (!_countryRepository.CountryExists(authortoCreate.Country.Id))
            {
                return(BadRequest());
            }

            authortoCreate.Country = _countryRepository.GetCountry(authortoCreate.Country.Id);

            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            if (!_authorRepository.CreateAuthor(authortoCreate))
            {
                return(BadRequest(ModelState));
            }

            return(CreatedAtRoute("GetAuthor", new { authorId = authortoCreate.Id }, authortoCreate));
        }
        public IActionResult GetCountry(int countryId)
        {
            // Will first check if a country does exists ?
            if (!_countryRepository.CountryExists(countryId))
            {
                return(NotFound());
            }

            // If a country exists it will assign it to the variable for further processes.
            var country = _countryRepository.GetCountry(countryId);

            // If a requst is invalid this will be executed.
            // Suppose instade of intager id string is inserted
            // This will show a bad request notification
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // If there is a country it will map the object with the ViewModel
            var countryDto = new CountryDto()
            {
                Id   = country.Id,
                Name = country.Name
            };

            // After successful execution it will show an Ok messaage with the result.
            return(Ok(countryDto));
        }
Esempio n. 6
0
        public IActionResult CreateAuthor([FromBody] Author authorToCreate)
        {
            if (authorToCreate == null)
            {
                return(BadRequest(ModelState));
            }

            if (!_countryRepository.CountryExists(authorToCreate.Country.Id))
            {
                ModelState.AddModelError("", "Country doesn't exist");
                return(StatusCode(404, ModelState));
            }

            authorToCreate.Country = _countryRepository.GetCountry(authorToCreate.Country.Id);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (!_authorRepository.CreateAuthor(authorToCreate))
            {
                ModelState.AddModelError("", $"Sonething went wrong saving the author " + $"{authorToCreate.FirstName} {authorToCreate.LastName}");
                return(StatusCode(500, ModelState)); //Server error
            }
            //After all went Ok, we want to route back to the source i.e. we will display the obejct which we wanted to save.
            return(CreatedAtRoute("GetAuthor", new { authorId = authorToCreate.Id }, authorToCreate));
        }
Esempio n. 7
0
        public IActionResult GetCountry(int countryId)
        {
            // Return Not Found Page if the country doesn't exist in the Database
            if (!_countryRepository.CountryExists(countryId))
            {
                return(NotFound());
            }

            var country = _countryRepository.GetCountry(countryId);

            // Check if Model State is valid or invalid
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var countryDto = new CountryDto()
            {
                Id   = country.Id,
                Name = country.Name
            };

            // Return status code with countries
            return(Ok(countryDto));
        }
Esempio n. 8
0
        public IActionResult CreateAuthor([FromBody] Author authorToCreate) //[FromBody] means all the info comes from the body of the post request.
        {
            if (authorToCreate == null)
            {
                return(BadRequest(ModelState));
            }

            if (!_countryRepository.CountryExists(authorToCreate.Country.Id))
            {
                ModelState.AddModelError("", "Country is not exist!");
                return(StatusCode(404, ModelState));
            }


            authorToCreate.Country = _countryRepository.GetCountry(authorToCreate.Country.Id);


            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }


            if (!_authorRepository.CreateAuthor(authorToCreate)) // here we try to add the new category to the DB.
            {
                // if the create was not success.
                ModelState.AddModelError("", $"Something want wrong saving {authorToCreate.FirstName} {authorToCreate.LastName} ");
                return(StatusCode(500, ModelState));
            }
            // if the create success.

            return(CreatedAtRoute("GetAuthor", new { authorId = authorToCreate.Id }, authorToCreate)); // return the info of the new category. call GetCategory() in line 57.
        }
Esempio n. 9
0
        public IActionResult GetCountry(int countryId)
        {
            if (!_countryRepository.CountryExists(countryId))
            {
                return(NotFound());
            }

            var country = _countryRepository.GetCountry(countryId);



            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }


            var countryDto = new CountryDto()
            {
                Id   = country.Id,
                Name = country.Name
            };

            return(Ok(countryDto));
        }
        public IActionResult GetCountry(int countryId)
        {
            if (!countryRepository.CountryExists(countryId))
            {
                return(NotFound());
            }
            var country    = countryRepository.GetCountry(countryId);
            var countryDTO = new CountryDTO()
            {
                Id   = country.Id,
                Name = country.Name
            };

            return(Ok(countryDTO));
        }
        private StatusCodeResult ValidateRecipe(List <int> authorsId, List <int> categoriesId, List <int> stepsId, List <int> ingredientsId, Recipe recipe)
        {
            recipe.Country = _countryRepository.GetCountry(recipe.Country.Id);

            if (recipe == null || authorsId.Count() <= 0 || categoriesId.Count() <= 0 || stepsId.Count() <= 0 || ingredientsId.Count() <= 0)
            {
                ModelState.AddModelError("", "Missing recipe, author, category, step, ingredient or country");
                return(BadRequest());
            }

            foreach (var id in authorsId)
            {
                if (!_authorRepository.AuthorExists(id))
                {
                    ModelState.AddModelError("", "Author Not Found");
                    return(StatusCode(404));
                }
            }

            foreach (var id in categoriesId)
            {
                if (!_categoryRepository.CategoryExists(id))
                {
                    ModelState.AddModelError("", "Category Not Found");
                    return(StatusCode(404));
                }
            }

            foreach (var id in stepsId)
            {
                if (!_stepRepository.StepExists(id))
                {
                    ModelState.AddModelError("", "Step Not Found");
                    return(StatusCode(404));
                }
            }

            foreach (var id in ingredientsId)
            {
                if (!_ingredientRepository.IngredientExists(id))
                {
                    ModelState.AddModelError("", "Ingredient Not Found");
                    return(StatusCode(404));
                }
            }

            if (!_countryRepository.CountryExists(recipe.Country.Id))
            {
                ModelState.AddModelError("", "Country does not exist");
                return(StatusCode(404));
            }

            if (!ModelState.IsValid)
            {
                ModelState.AddModelError("", "Critical Error");
                return(BadRequest());
            }

            return(NoContent());
        }
Esempio n. 12
0
        public IActionResult CreateAuthor([FromBody] Author authorToCerate)
        {
            if (authorToCerate == null)
            {
                return(BadRequest(ModelState));
            }

            if (!_countryRepository.CountryExists(authorToCerate.Country.Id))
            {
                ModelState.AddModelError("", $"The country with Id {authorToCerate.Country.Id} doesn't exist.");
                return(NotFound(ModelState));
            }

            authorToCerate.Country = _countryRepository.GetCountry(authorToCerate.Country.Id);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }


            if (!_authorRepository.CreateAuthor(authorToCerate))
            {
                ModelState.AddModelError("", $"While creating author some error occur.please try again.");
                return(StatusCode(500, ModelState));
            }
            return(CreatedAtRoute("GetAuthor", new { authorId = authorToCerate.Id }, authorToCerate));
        }
        public IActionResult CreateAuthor(Author newAuthor)
        {
            if (newAuthor == null)
            {
                return(BadRequest(ModelState));
            }

            // Check if the Author's Country is Valid
            if (!_countryRepository.CountryExists(newAuthor.Country.Id))
            {
                ModelState.AddModelError("", "Country doesn't exist");
                return(StatusCode(404, ModelState));
            }

            // Store the FULL Country object in newAuthor
            newAuthor.Country = _countryRepository.GetCountry(newAuthor.Country.Id);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // Then I can try to create the Author and add it to the database
            if (!_authorRepository.CreateAuthor(newAuthor))
            {
                ModelState.AddModelError("", $"Something went wrong while trying to create {newAuthor.FirstName} {newAuthor.LastName}");
                return(StatusCode(500, ModelState));
            }

            // If we reach this point, the author has been added to the database. I can return the new author object
            return(CreatedAtRoute("GetAuthor", new { authorId = newAuthor.Id }, newAuthor));
        }
        public IActionResult CreateAuthor([FromBody] Author authorToCreate)
        {
            if (authorToCreate == null)
            {
                return(BadRequest(ModelState));
            }

            var author = authorRepository.GetAuthors()
                         .Where(a => a.LastName.Trim().ToUpper() == authorToCreate.LastName.Trim().ToUpper() &&
                                a.FirstName.Trim().ToUpper() == authorToCreate.FirstName.Trim().ToUpper())
                         .FirstOrDefault();

            if (author != null)
            {
                ModelState.AddModelError("", $"{authorToCreate.FirstName} {authorToCreate.FirstName} already exists");
                return(StatusCode(422, ModelState));
            }

            if (!countryRepository.CountryExists(authorToCreate.Country.Id))
            {
                ModelState.AddModelError("", "Country doesn't exist!");
            }

            authorToCreate.Country = countryRepository.GetCountry(authorToCreate.Country.Id);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!authorRepository.CreateAuthor(authorToCreate))
            {
                ModelState.AddModelError("", $"Something went wrong creating new author");
                return(StatusCode(500, ModelState));
            }

            return(CreatedAtRoute("GetAuthor", new { authorId = authorToCreate.Id }, authorToCreate));
        }
        public IActionResult GetCountry(int countryId)
        {
            if (!_countryRepository.CountryExists(countryId))
            {
                return(NotFound());
            }


            var country = _countryRepository.GetCountry(countryId);

            if (!ModelState.IsValid) // if the call was invalid, then return bad request. (if the list was empty that doesn't mean it's invalid result).
            {
                return(BadRequest(ModelState));
            }

            var countryDto = new CountryDto()
            {
                Id   = country.Id,
                Name = country.Name
            };

            return(Ok(countryDto));
        }
Esempio n. 16
0
        public async Task <IActionResult> GetCitiesForCountry(Guid countryId)
        {
            if (!await countryRepository.CountryExists(countryId))
            {
                return(NotFound());
            }

            var cities = await cityRepository.GetCitiesAsync(countryId);

            var citiesResource = mapper.Map <IEnumerable <CityResource> >(cities);

            citiesResource = citiesResource.Select(CreateLinksFactory);
            var wrapper = new LinkCollectionResourceWrapper <CityResource>(citiesResource);

            return(Ok(CreateLinksForCities(wrapper)));
        }
Esempio n. 17
0
        public async Task <IActionResult> GetCountry(int countryId)
        {
            if (!await _countryRepository.CountryExists(countryId))
            {
                return(StatusCode(StatusCodes.Status404NotFound));
            }

            var countries = await _countryRepository.GetCountry(countryId);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var countriesDto = new CountryDTO()
            {
                Id   = countries.Id,
                Name = countries.Name
            };


            return(Ok(countriesDto));
        }
Esempio n. 18
0
 /// <summary>
 /// Metodo para verificar si existe un pais
 /// </summary>
 /// <param name="countryId">Id del pais buscado</param>
 /// <returns>Treu si existe o false en caso contrario</returns>
 public bool CountryExists(int countryId)
 {
     return(_repository.CountryExists(countryId));
 }