public async Task <IActionResult> Upsert(NationalPark nationalPark)
        {
            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();

                    nationalPark.Picture = p1;
                }
                else
                {
                    var objFromdb = await nationalParkRepository.GetAsync(SD.NationalParkAPIUrl, nationalPark.Id, HttpContext.Session.GetString("JWTToken"));

                    nationalPark.Picture = objFromdb.Picture;
                }

                if (nationalPark.Id == 0)
                {
                    await nationalParkRepository.CreateAsync(SD.NationalParkAPIUrl, nationalPark, HttpContext.Session.GetString("JWTToken"));
                }
                else
                {
                    await nationalParkRepository.UpdateAsync(SD.NationalParkAPIUrl + nationalPark.Id, nationalPark, HttpContext.Session.GetString("JWTToken"));
                }
            }
            return(View(nationalPark));
        }
        public async Task <IActionResult> Upsert(NationalPark nationalPark)
        {
            if (ModelState.IsValid)
            {
                var files = HttpContext.Request.Form.Files;
                if (files.Count > 0)
                {
                    nationalPark.Picture = SD.GetByteArrayFromImage(files[0]);
                }
                else
                {
                    var npFromDb = await _npRepository.GetAsync(SD.NationalParkAPIPath, nationalPark.Id, Token);

                    nationalPark.Picture = npFromDb.Picture;
                }

                if (nationalPark.Id == 0)
                {
                    await _npRepository.CreateAsync(SD.NationalParkAPIPath, nationalPark, Token);
                }
                else
                {
                    await _npRepository.UpdateAsync(SD.NationalParkAPIPath + nationalPark.Id, nationalPark, Token);
                }

                return(RedirectToAction(nameof(Index)));
            }
            else
            {
                return(View(nationalPark));
            }
        }
Пример #3
0
        public async Task <IActionResult> Upsert(NationalPark national)
        {
            if (national == null)
            {
                return(View(national));
            }

            if (ModelState.IsValid)
            {
                var files = HttpContext.Request.Form.Files;
                if (files.Count > 0)
                {
                    byte[] p1 = null;
                    using (var fo = files[0].OpenReadStream())
                    {
                        using (var mo = new MemoryStream())
                        {
                            fo.CopyTo(mo);
                            p1 = mo.ToArray();
                        }
                    }
                    national.Picture = p1;
                }
                else
                {
                    var objectform = await _npRepo.GetByIdAsync(national.Id, SD.APIBaseUrl);

                    national.Picture = objectform.Picture;
                }

                if (national.Id == 0)
                {
                    if (await _npRepo.CreateAsync(national, SD.NationalParkAPIPath))
                    {
                        return(RedirectToAction(nameof(Index)));
                    }
                    else
                    {
                        return(View(national));
                    }
                }
                else
                {
                    if (await _npRepo.UpdateAsync(national, SD.NationalParkAPIPath + national.Id))
                    {
                        return(RedirectToAction(nameof(Index)));
                    }
                    else
                    {
                        return(View(national));
                    }
                }
            }

            return(View(national));
        }
Пример #4
0
        public async Task <IActionResult> Upsert(NationalPark obj)
        {
            //if true all required data was provided properly
            if (ModelState.IsValid)
            {
                //processing the uploaded files
                var files = HttpContext.Request.Form.Files;
                //if true image was uploaded
                if (files.Count > 0)
                {
                    //img converting to string
                    byte[] p1 = null;
                    //opening the file
                    using (var fs1 = files[0].OpenReadStream())
                    {
                        //converting the img to array of bytes
                        using (var ms1 = new MemoryStream())
                        {
                            fs1.CopyTo(ms1);
                            p1 = ms1.ToArray();
                        }
                    }
                    //picture == array of bytes
                    obj.Picture = p1;
                }
                else
                {
                    //retrive obj from db and get the image
                    var objFromDb = await _npRepo.GetAsync(StaticDetails.NationalParkAPIPath, obj.Id);

                    obj.Picture = objFromDb.Picture;
                }
                if (obj.Id == 0)
                {
                    //update expects id
                    await _npRepo.CreateAsync(StaticDetails.NationalParkAPIPath + obj.Id, obj);
                }
                return(RedirectToAction(nameof(Index)));
            }
            //if the model state is not valid
            else
            {
                return(View(obj));
            }
        }
        public async Task <IActionResult> Upsert(NationalPark obj)
        {
            if (ModelState.IsValid)
            {
                //get files uploaded
                var files = HttpContext.Request.Form.Files;
                if (files.Count > 0) //means file was uploaded
                {
                    byte[] pic1 = null;
                    using (var fileStream1 = files[0].OpenReadStream())
                    {
                        using (var memoryStream1 = new MemoryStream())
                        {
                            fileStream1.CopyTo(memoryStream1);
                            pic1 = memoryStream1.ToArray();
                        }
                    }
                    obj.Picture = pic1;
                }
                else
                {
                    var objFromDb = await _npRepo.GetAsync(SD.NationalParkAPIPath, obj.Id, HttpContext.Session.GetString("JWToken"));

                    obj.Picture = objFromDb.Picture;
                }

                if (obj.Id == 0)
                {
                    //create
                    await _npRepo.CreateAsync(SD.NationalParkAPIPath, obj, HttpContext.Session.GetString("JWToken"));
                }
                else
                {
                    //update
                    await _npRepo.UpdateAsync(SD.NationalParkAPIPath + obj.Id, obj, HttpContext.Session.GetString("JWToken"));
                }

                return(RedirectToAction(nameof(Index)));
            }
            else
            {
                return(View(obj));
            }
        }
Пример #6
0
        public async Task <IActionResult> Upsert(NationalPark obj)
        {
            if (ModelState.IsValid)
            {
                var files = HttpContext.Request.Form.Files;
                //Check if file was uploaded
                if (files.Count > 0)
                {
                    byte[] p1 = null;
                    //This will read the file
                    using (var fs1 = files[0].OpenReadStream())
                    {
                        using (var ms1 = new MemoryStream())
                        {
                            fs1.CopyTo(ms1);
                            p1 = ms1.ToArray();
                        }
                    }
                    obj.Picture = p1;
                }
                else
                {
                    //If no img uploaded. Get img from DB
                    var objFromDb = await _npRepo.GetAsync(SD.NationalParkAPIPath, obj.Id, HttpContext.Session.GetString("JWToken"));

                    obj.Picture = objFromDb.Picture;
                }
                if (obj.Id == 0)
                {
                    //Create
                    await _npRepo.CreateAsync(SD.NationalParkAPIPath, obj, HttpContext.Session.GetString("JWToken"));
                }
                else
                {
                    await _npRepo.UpdateAsync(SD.NationalParkAPIPath + obj.Id, obj, HttpContext.Session.GetString("JWToken"));
                }
                return(RedirectToAction(nameof(Index)));
            }
            else
            {
                //Return back to he view with object
                return(View(obj));
            }
        }
Пример #7
0
        public async Task <IActionResult> Upsert(NationalPark nationalPark)
        {
            if (ModelState.IsValid)
            {
                /*
                 * An image file can be uploaded both during the create new or edit record.
                 * from the incoming forms object, get the file convert from a stream to memory stream of array and
                 * parse it to the national park picture object as a byte.
                 */
                var files = HttpContext.Request.Form.Files;
                if (files.Count > 0)
                {
                    byte[] picture1 = null;
                    using (var file = files[0].OpenReadStream())
                    {
                        using (var memoryStream = new MemoryStream())
                        {
                            file.CopyTo(memoryStream);
                            picture1 = memoryStream.ToArray();
                        }
                    }
                    nationalPark.Picture = picture1;
                }
                else
                {
                    var nationalParkObjectformDb = await _nationalPark.GetAsync(StaticDetails.NationalParkApiUrl, nationalPark.Id);

                    nationalPark.Picture = nationalParkObjectformDb.Picture;
                }
                if (nationalPark.Id == 0)
                {
                    await _nationalPark.CreateAsync(StaticDetails.NationalParkApiUrl, nationalPark);
                }
                else
                {
                    await _nationalPark.UpdateAsync(StaticDetails.NationalParkApiUrl + nationalPark.Id, nationalPark);
                }
                return(RedirectToAction(nameof(Index)));
            }
            else
            {
                return(View(nationalPark));
            }
        }
Пример #8
0
        public async Task <IActionResult> Upsert(NationalPark park)
        {
            if (ModelState.IsValid)
            {
                var    files = HttpContext.Request.Form.Files;
                byte[] pi    = null;
                if (files.Count > 0)
                {
                    using (var fsi = files[0].OpenReadStream())
                    {
                        using (var msi = new MemoryStream())
                        {
                            fsi.CopyTo(msi);
                            pi = msi.ToArray();
                        }
                    }
                    park.Image = pi;
                }
                else
                {
                    var parkFromDb = await _repository.GetAsync(Globals.ApiNpUrl, park.Id, HttpContext.Session.GetString("JWToken"));

                    if (parkFromDb != null)
                    {
                        park.Image = parkFromDb.Image;
                    }
                }
                if (park.Id == 0)
                {
                    await _repository.CreateAsync(Globals.ApiNpUrl, park, HttpContext.Session.GetString("JWToken"));
                }
                else
                {
                    await _repository.UpdateAsync(Globals.ApiNpUrl, park, park.Id, HttpContext.Session.GetString("JWToken"));
                }

                return(RedirectToAction(nameof(Index)));
            }
            else
            {
                return(View(park));
            }
        }
Пример #9
0
        public async Task <IActionResult> Upsert(NationalPark obj)
        {
            if (ModelState.IsValid)
            {
                // Get The image from file input
                var files = HttpContext.Request.Form.Files;
                if (files.Count > 0) // if choose one
                {
                    byte[] p1 = null;
                    using (var fs1 = files[0].OpenReadStream())
                    {
                        using (var ms1 = new MemoryStream())
                        {
                            fs1.CopyTo(ms1);
                            p1 = ms1.ToArray();
                        }
                    }
                    obj.Picture = p1;
                }
                else // if don't need to change
                {
                    var objFromDb = await _npRepo.GetAsync(SD.NationalParkAPIPath, obj.Id, HttpContext.Session.GetString("JWToken"));

                    obj.Picture = objFromDb.Picture;
                }

                // create or update the object
                if (obj.Id == 0)
                {
                    await _npRepo.CreateAsync(SD.NationalParkAPIPath, obj, HttpContext.Session.GetString("JWToken"));
                }
                else
                {
                    await _npRepo.UpdateAsync(SD.NationalParkAPIPath + obj.Id, obj, HttpContext.Session.GetString("JWToken"));
                }
                return(RedirectToAction(nameof(Index)));
            }
            else
            {
                return(View(obj));
            }
        }
        public async Task <IActionResult> Upsert(NationalPark nationalPark)
        {
            if (!ModelState.IsValid)
            {
                return(View(nationalPark));
            }

            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();
                    }
                }

                nationalPark.Picture = p1;
            }
            else
            {
                var objFromDb = await _npRepo.GetAsync(StaticDetails.NationalParksApiPath, nationalPark.Id, GetToken());

                nationalPark.Picture = objFromDb.Picture;
            }

            if (nationalPark.Id == 0)
            {
                await _npRepo.CreateAsync(StaticDetails.NationalParksApiPath, nationalPark, GetToken());
            }
            else
            {
                await _npRepo.UpdateAsync(StaticDetails.NationalParksApiPath + nationalPark.Id, nationalPark,
                                          GetToken());
            }

            return(RedirectToAction(nameof(Index)));
        }
Пример #11
0
        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.CopyToAsync(ms1);
                            p1 = ms1.ToArray();
                        }
                    }

                    obj.Picture = p1;
                }
                else
                {
                    var objFromDb = await _nationalParkRepository.GetAsync(SD.NationalAPIPath, obj.Id, HttpContext.Session.GetString("JWToken"));

                    obj.Picture = objFromDb.Picture;
                }

                if (obj.Id == 0)
                {
                    await _nationalParkRepository.CreateAsync(SD.NationalAPIPath, obj, HttpContext.Session.GetString("JWToken"));
                }
                else
                {
                    await _nationalParkRepository.UpdateAsync(SD.NationalAPIPath + obj.Id, obj, HttpContext.Session.GetString("JWToken"));
                }

                return(RedirectToAction(nameof(Index)));
            }
            else
            {
                return(View(obj));
            }
        }
        public async Task <IActionResult> Create(NationalParkCreateDto model, IFormFile Picture)
        {
            if (ModelState.IsValid)
            {
                //var files = HttpContext.Request.Form.Files; // to get the uploaded files without specify parameter to recieve them
                if (Picture != null)
                {
                    using (var ms = new MemoryStream())
                    {
                        Picture.CopyTo(ms);
                        model.Picture = ms.ToArray();
                    }
                }

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

                if (success)
                {
                    return(RedirectToAction("Index"));
                }
                ModelState.AddModelError("", "SomeError happens.");
            }
            return(View(model));
        }
Пример #13
0
 private async Task CreateNationalPark(NationalPark nationalPark)
 {
     await _nationalParkRepository.CreateAsync(AppConstants.NationalParkApiPath, nationalPark);
 }
Пример #14
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));
                }
            }
        }