public async Task <IActionResult> CreateCountry([FromBody] CountryAddVM country)
        {
            if (country == null)
            {
                return(BadRequest());
            }
            var countryModel = _mapper.Map <Country>(country);

            _countryRepository.AddCountry(countryModel);
            if (!await _unitOfWork.SaveAsync())
            {
                return(StatusCode(500, "数据添加失败!"));
            }
            var countryVM = _mapper.Map <CountryVM>(countryModel);

            return(CreatedAtAction(nameof(GetCountry), new { id = countryVM.Id }, countryVM));
        }
Example #2
0
        private void BtnAddUser_Click(object sender, RoutedEventArgs e)
        {
            string url            = "http://localhost:51160/api/country";
            var    httpWebRequest = (HttpWebRequest)WebRequest.Create(url);

            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method      = "POST";

            CountryAddVM country = new CountryAddVM
            {
                Name      = "Niger",
                FlagImage = "https://static.drukukr.com/catalog/14/world-00286__1446562318__615.jpg"
            };

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                string json = JsonConvert.SerializeObject(country);
                //"{\"user\":\"test\"," +
                //"\"password\":\"bla\"}";
                streamWriter.Write(json);
            }
            try
            {
                String resultStr = String.Empty;
                using (HttpWebResponse response =
                           (HttpWebResponse)httpWebRequest.GetResponse())
                {
                    Stream       dataStream = response.GetResponseStream();
                    StreamReader reader     = new StreamReader(dataStream);
                    resultStr = reader.ReadToEnd();
                    reader.Close();
                    dataStream.Close();
                }
                var resCountry = JsonConvert.DeserializeObject <CountryVM>(resultStr);
                _countries.Add(resCountry);
            }
            catch
            {
                MessageBox.Show("Помилка додавання");
            }
        }
Example #3
0
        public async Task <IActionResult> CreateAsync(CountryAddVM model)
        {
            if (ModelState.IsValid)
            {
                var serverPath = _env.ContentRootPath; //Directory.GetCurrentDirectory(); //_env.WebRootPath;
                var folerName  = @"\wwwroot\Uploads\";
                //var path = Path.Combine(serverPath, folerName); //
                var path = serverPath + folerName; //
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                string ext      = Path.GetExtension(model.Flag.FileName);
                string fileName = Path.GetRandomFileName() + ext;

                string filePathSave = Path.Combine(path, fileName);

                // сохраняем файл в папку Files в каталоге wwwroot
                using (var fileStream = new FileStream(filePathSave, FileMode.Create))
                {
                    await model.Flag.CopyToAsync(fileStream);
                }

                var country = _countryRepos.GetAll().FirstOrDefault(c => c.Name == model.Name);
                if (country == null)
                {
                    var result = _countryRepos.Create
                                     (new Country
                    {
                        Name       = model.Name,
                        Flag       = fileName,
                        DateCreate = DateTime.Now
                    });
                }

                return(RedirectToAction("Index"));
            }
            ModelState.AddModelError("", "Дані вказано не коректно");
            return(View(model));
        }