public async Task <IActionResult> Edit(NationalParkUpdateDto model, IFormFile PictureFile)
        {
            if (ModelState.IsValid)
            {
                if (PictureFile != null)
                {
                    using (var ms = new MemoryStream())
                    {
                        PictureFile.CopyTo(ms);
                        model.Picture = ms.ToArray();
                    }
                }

                var success = await _npRepo.UpdateAsync(_npUrl, model.Id, model, HttpContext.Session.GetString("JWToken"));

                if (success)
                {
                    return(RedirectToAction(nameof(Index)));
                }
                ModelState.AddModelError("", "SomeError happens.");
            }
            return(View(model));
        }
        public async Task <IActionResult> Upsert(NationalPark obj)
        {
            if (ModelState.IsValid)
            {
                var files = HttpContext.Request.Form.Files;
                if (files.Count > 0)
                {
                    byte[] p1 = null;
                    using var fs1 = files[0].OpenReadStream();
                    using var ms1 = new MemoryStream();
                    fs1.CopyTo(ms1);
                    p1 = ms1.ToArray();

                    obj.Picture = p1;
                }
                else
                {
                    var objFromDb = await _npRepo.GetAsync(SD.NationalParkAPIPath, obj.Id, HttpContext.Session.GetString("JWTToken"));

                    obj.Picture = objFromDb.Picture;
                }
                if (obj.Id == 0)
                {
                    await _npRepo.CreateAsync(SD.NationalParkAPIPath, obj, HttpContext.Session.GetString("JWTToken"));
                }
                else
                {
                    await _npRepo.UpdateAsync(SD.NationalParkAPIPath + obj.Id, obj, HttpContext.Session.GetString("JWTToken"));
                }
                return(RedirectToAction(nameof(Index)));
            }
            else
            {
                return(View(obj));
            }
        }
 private async Task UpdateNationalPark(NationalPark nationalPark)
 {
     await _nationalParkRepository.UpdateAsync(AppConstants.NationalParkApiPath, nationalPark.Id, nationalPark);
 }
Beispiel #4
0
        public async Task <IActionResult> Upsert(NationalPark nationalPark)
        {
            var succeed = false;
            var files   = HttpContext.Request.Form.Files;
            var token   = HttpContext.Session.GetString("JwToken");

            if (!ModelState.IsValid)
            {
                return(View(nationalPark));
            }

            if (files.Count > 0)
            {
                byte[] img1 = null;
                using (var fs1 = files[0].OpenReadStream())
                {
                    using (var ms1 = new MemoryStream())
                    {
                        fs1.CopyTo(ms1);
                        img1 = ms1.ToArray();
                    }
                }
                nationalPark.Image = img1;
            }
            else
            {
                var objFromDb = await _npRepository.GetAsync(SD.NationalParkAPIPath, nationalPark.Id, token);

                if (objFromDb != null && objFromDb.Image != null)
                {
                    nationalPark.Image = objFromDb.Image;
                }
            }

            if (nationalPark.Id == 0)
            {
                succeed = await _npRepository.CreateAsync(SD.NationalParkAPIPath, nationalPark, token);

                if (succeed)
                {
                    return(RedirectToAction("Index"));
                }
                else
                {
                    return(View(nationalPark));
                }
            }
            else
            {
                //var objFromDb2 = await _npRepository.GetAsync(SD.NationalParkAPIPath, nationalPark.Id);
                //nationalPark.CreatedDate = objFromDb2.CreatedDate;

                succeed = await _npRepository.UpdateAsync(SD.NationalParkAPIPath, nationalPark.Id, nationalPark, token);

                if (succeed)
                {
                    return(RedirectToAction("Index"));
                }
                else
                {
                    return(View(nationalPark));
                }
            }
        }