Exemple #1
0
        public async Task <IActionResult> Create([Bind("Id,CountryId,TeamId,Name,Birthdate")] Player player)
        {
            if (ModelState.IsValid)
            {
                if (!Valid.IsAllLetters(player.Name))
                {
                    return(RedirectToAction(nameof(Index)));
                }
                _context.Add(player);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CountryId"] = new SelectList(_context.Countries, "Id", "Name", player.CountryId);
            ViewData["TeamId"]    = new SelectList(_context.Teams, "Id", "Name", player.TeamId);
            return(View(player));
        }
        public async Task <IActionResult> Import(IFormFile fileExcel)
        {
            if (ModelState.IsValid)
            {
                if (fileExcel != null)
                {
                    using (var stream = new FileStream(fileExcel.FileName, FileMode.Create))
                    {
                        await fileExcel.CopyToAsync(stream);

                        using (XLWorkbook workBook = new XLWorkbook(stream, XLEventTracking.Disabled))
                        {
                            foreach (IXLWorksheet worksheet in workBook.Worksheets)
                            {
                                if (!Valid.IsAllLetters(worksheet.Name))
                                {
                                    continue;
                                }
                                Country newcoun;
                                var     c = (from coun in _context.Countries
                                             where coun.Name.Contains(worksheet.Name)
                                             select coun).ToList();

                                if (c.Count > 0)
                                {
                                    newcoun = c[0];
                                }

                                else
                                {
                                    newcoun      = new Country();
                                    newcoun.Name = worksheet.Name;

                                    _context.Countries.Add(newcoun);
                                }

                                foreach (IXLRow row in worksheet.RowsUsed().Skip(1))
                                {
                                    try
                                    {
                                        Organisation organisation = new Organisation();
                                        organisation.Name = row.Cell(1).Value.ToString();



                                        organisation.CreationDate = DateTime.Parse(row.Cell(2).Value.ToString());
                                        if (!Valid.Datecheck(organisation.CreationDate))
                                        {
                                            continue;
                                        }
                                        //int Day = Int.Parse(row.Cell(2).Value.ToString());
                                        //int Mounth = Int.Parse(row.Cell(3).Value.ToString());
                                        //int Year = Int.Parse(row.Cell(4).Value.ToString());
                                        //organisation.CreationDate = new DateTime(Year, Mounth, Day);
                                        organisation.Country = newcoun;
                                        var dcp = (from org in _context.Organisations
                                                   where org.Name.Contains(organisation.Name)
                                                   select org).ToList();
                                        if (dcp.Count > 0)
                                        {
                                            continue;
                                        }
                                        _context.Organisations.Add(organisation);
                                    }
                                    catch (Exception e)
                                    {
                                    }
                                }
                            }
                        }
                    }
                }

                await _context.SaveChangesAsync();
            }
            return(RedirectToAction(nameof(Index)));
        }