Exemple #1
0
        public IActionResult Create([FromBody] CountryAddViewModel model)
        {
            var country = _context.Countries
                          .SingleOrDefault(c => c.Name == model.Name);

            if (country != null)
            {
                return(BadRequest(new { invalid = "Така країна уже є в БД!" }));
            }

            string fileDestDir = _env.ContentRootPath;

            fileDestDir = Path.Combine(fileDestDir, "Bigimot");
            string imageName = Path.GetRandomFileName() + ".jpg";

            var bitmap = model.Image.Split(',')[1].FromBase64StringToImage();

            var outbtmp = ImageWorker.CreateImage(bitmap, 100, 100);

            outbtmp.Save(Path.Combine(fileDestDir, imageName), ImageFormat.Jpeg);
            //bitmap.Save(Path.Combine(fileDestDir,imageName), ImageFormat.Jpeg);

            country = new Country
            {
                Name      = model.Name,
                FlagImage = model.FlagImage
            };
            _context.Countries.Add(country);
            _context.SaveChanges();
            return(Ok(new CountryViewModel
            {
                Id = country.Id,
                Name = country.Name
            }));
        }
Exemple #2
0
 public ActionResult Create(CountryAddViewModel country)
 {
     if (ModelState.IsValid)
     {
         var status = _locationProvider.CountryAdd(country);
         if (status == StatusCountryViewModel.Success)
         {
             return(RedirectToAction("Index"));
         }
         else if (status == StatusCountryViewModel.Dublication)
         {
             ModelState.AddModelError("", "Країна за даним іменем уже існує!");
         }
     }
     return(View(country));
 }
        public async Task <IActionResult> Post([FromBody] CountryAddViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var country = new Country
            {
                Name = model.Name
            };

            _context.Countries.Add(country);


            await _context.SaveChangesAsync();

            return(Ok(new { country.Id, country.Name }));
        }
Exemple #4
0
        public StatusCountryViewModel CountryAdd(CountryAddViewModel addCountry)
        {
            var searchCountry = _countryRepository.GetCountryByName(addCountry.Name);

            if (searchCountry != null)
            {
                return(StatusCountryViewModel.Dublication);
            }

            Country country = new Country
            {
                Name       = addCountry.Name,
                DateCreate = DateTime.Now,
                Priority   = addCountry.Priority
            };

            _countryRepository.Add(country);
            _countryRepository.SaveChange();

            return(StatusCountryViewModel.Success);
        }
Exemple #5
0
        public async Task <IActionResult> PostNew([FromBody] CountryAddViewModel country)
        {
            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResultCus(ModelState));
            }

            var countryModel = mapper.Map <Country>(country);

            await countryRepository.AddCountryAsync(countryModel);

            if (!await unitOfWork.SaveAsync())
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Error when Add"));
            }

            var countryResource = mapper.Map <CountryResource>(countryModel);
            var links           = CreateCountryLinks(countryModel.Id);
            var result          = countryResource.ToDynamic() as IDictionary <string, object>;

            result.Add("links", links);

            return(CreatedAtAction(nameof(GetCountry), new { id = countryResource.Id }, result));
        }