示例#1
0
 /*      public async Task<IActionResult> OnPostAsync()
  *    {
  *        if (!ModelState.IsValid)
  *        {
  *            return Page();
  *        }
  *
  *        var EmptyLocation = new Location();
  *
  *        if (await TryUpdateModelAsync(
  *                 EmptyLocation,
  *                 "Location",
  *                  l => l.ProvinceID, l => l.LocationName,
  *                  l => l.GPSCoordinates, l=> l.Province.CountryID
  *                     ))
  *        {
  *            _context.Locations.Add(EmptyLocation);
  *            await _context.SaveChangesAsync();
  *            return RedirectToPage("./Index");
  *        }
  *        //Select Province ID if TryUpdateSync Fails
  *        PopulateCountryDropDownList(_context, EmptyLocation.Province.CountryID);
  *        PopulateProvinceDropDownList(_context, EmptyLocation.ProvinceID);
  *       return Page();
  *
  *    }
  *
  *    public async Task<IActionResult> OnPostAsyncLocation([FromBody] Location obj)
  *    {
  *        if (!ModelState.IsValid)
  *        {
  *            return new JsonResult("Location Data is incomplete or inaccurate");
  *        }
  *
  *        var EmptyLocation = new Location();
  *
  *        if (await TryUpdateModelAsync(
  *                 EmptyLocation,
  *                 "Location",
  *                  l => l.ProvinceID, l => l.LocationName,
  *                  l => l.GPSCoordinates
  *                     ))
  *        {
  *            _context.Locations.Add(EmptyLocation);
  *            await _context.SaveChangesAsync();
  *            return new JsonResult("New Location Added");
  *        }
  *
  *        return new JsonResult("Location Not Added");
  *
  *    }
  */
 public IActionResult OnPostInsert([FromBody] Location obj)
 {
     if (HttpContext.User.IsInRole("Fleet") || (HttpContext.User.IsInRole("Admin")))
     {
         _context.Add(obj);
         _context.SaveChanges();
         return(new JsonResult("Location: " + obj.LocationName + " added."));
     }
     return(new JsonResult("You do not have access to add Locations"));
 }
示例#2
0
 public IActionResult OnPutUpdate([FromBody] Location obj)
 {
     if (HttpContext.User.IsInRole("Fleet") || (HttpContext.User.IsInRole("Admin")))
     {
         _context.Attach(obj).State = EntityState.Modified;
         _context.SaveChanges();
         return(new JsonResult("Location: " + obj.LocationName + " Changes are saved."));
     }
     return(new JsonResult("You do not have access to add new Locations"));
 }
示例#3
0
        //[DisableFormValueModelBinding]
        //[ValidateAntiForgeryToken]
        public string ImportUpload(IFormFile reportfile)
        {
            string folderName  = "UploadPrices";
            string webRootPath = _hostingEnvironment.WebRootPath;
            string newPath     = Path.Combine(webRootPath, folderName);

            // Delete Files from Directory
            System.IO.DirectoryInfo di = new DirectoryInfo(newPath);
            foreach (FileInfo filesDelete in di.GetFiles())
            {
                filesDelete.Delete();
            }// End Deleting files form directories

            if (!Directory.Exists(newPath))// Crate New Directory if not exist as per the path
            {
                Directory.CreateDirectory(newPath);
            }
            var fiName = Guid.NewGuid().ToString() + Path.GetExtension(reportfile.FileName);

            using (var fileStream = new FileStream(Path.Combine(newPath, fiName), FileMode.Create))
            {
                reportfile.CopyTo(fileStream);
            }
            // Get uploaded file path with root
            string   rootFolder = _hostingEnvironment.WebRootPath;
            string   fileName   = @"UploadPrices/" + fiName;
            FileInfo file       = new FileInfo(Path.Combine(rootFolder, fileName));

            using (ExcelPackage package = new ExcelPackage(file))
            {
                ExcelWorksheet workSheet = package.Workbook.Worksheets[1];
                int            totalRows = workSheet.Dimension.Rows;
                List <CicotiWebApp.Models.Price> reportList = new List <CicotiWebApp.Models.Price>();
                for (int i = 2; i <= totalRows; i++)
                {
                    try
                    {
                        //string Title = workSheet?.Cells[i, 1]?.Value?.ToString();
                        //string Url = workSheet?.Cells[i, 2]?.Value?.ToString();
                        int      SKUID        = int.Parse(workSheet?.Cells[i, 1]?.Value?.ToString());
                        int      PriceTypeID  = int.Parse(workSheet?.Cells[i, 2]?.Value?.ToString());
                        double   PriceInclVat = double.Parse(workSheet?.Cells[i, 3]?.Value?.ToString());
                        double   PriceExlcVat = double.Parse(workSheet?.Cells[i, 4]?.Value?.ToString());
                        int      RegionID     = int.Parse(workSheet?.Cells[i, 5]?.Value?.ToString());
                        int      BranchID     = int.Parse(workSheet?.Cells[i, 6]?.Value?.ToString());
                        DateTime PriceDate    = DateTime.Parse(workSheet?.Cells[i, 7]?.Value?.ToString());
                        reportList.Add(new CicotiWebApp.Models.Price
                        {
                            SKUID          = SKUID
                            , PriceTypeID  = PriceTypeID
                            , PriceInclVat = PriceInclVat
                            , PriceExlcVat = PriceExlcVat
                            , RegionID     = RegionID
                            , BranchID     = BranchID
                            , PriceDate    = PriceDate
                                             //  SKUID = Title,
                                             //  Url = Url,
                        });
                    }
                    catch (Exception Ex)
                    {
                        // Exception
                    }
                }

                _context.Prices.AddRange(reportList);
                _context.SaveChanges();

                return("Uploaded");
            }
        }