public void ProcessData(CsvReader reader, Type type) { var instance = Activator.CreateInstance(type); var properties = type.GetProperties(); for (var i = 0; i < properties.Length; i++) { if (!_headers.Contains(properties[i].Name)) { continue; } var typeProperty = properties[i].PropertyType.Name; switch (typeProperty) { case "Int32": properties[i].SetValue(instance, int.Parse(reader[_namePosition[properties[i].Name]])); break; case "Decimal": properties[i].SetValue(instance, decimal.Parse(reader[_namePosition[properties[i].Name]])); break; default: properties[i].SetValue(instance, reader[_namePosition[properties[i].Name]]); break; } } switch (type.Name) { case "Person": _context.Add <Person>(instance as Person); break; case "Product": _context.Add(instance as Product); break; default: break; } _context.SaveChanges(); }
public async Task <IActionResult> Create([Bind("ID,Name,Email,Password,Title,Phone")] Employee employee) { if (ModelState.IsValid) { _context.Add(employee); await _context.SaveChangesAsync(); return(RedirectToAction("Index")); } return(View(employee)); }
public async Task <IActionResult> Create([Bind("Id,FirstName,LastName,Age")] Person person) { if (ModelState.IsValid) { _context.Add(person); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(person)); }
public async Task <IActionResult> Create([Bind("SkillID,EmployeeID,Description")] Skill skill) { if (ModelState.IsValid) { _context.Add(skill); await _context.SaveChangesAsync(); return(RedirectToAction("Index")); } ViewData["EmployeeID"] = new SelectList(_context.Employee, "ID", "ID", skill.EmployeeID); return(View(skill)); }
public async Task <IActionResult> Apply([Bind("DateDebut, DateFin, CongeTypeID")] Conge conge) { var viewModel = new CongeFormulaire { CongeTypes = await _context.CongeTypes .Where(t => t.Label != Types.Absence) .AsNoTracking() .ToListAsync(), Conge = conge }; var currentUserId = _userManager.GetUserId(HttpContext.User); Dictionary <int, int> yearQuota = new Dictionary <int, int>(); yearQuota.Add(conge.DateDebut.Year, 0); if (conge.DateDebut.Year != conge.DateFin.Year) { yearQuota.Add(conge.DateFin.Year, 0); } var conges = await _context.Conges .Where(c => c.CollaborateurID == currentUserId) .Where(c => (c.DateDebut.Year == conge.DateDebut.Year || c.DateDebut.Year == conge.DateFin.Year) || (c.DateFin.Year == conge.DateDebut.Year || c.DateFin.Year == conge.DateFin.Year)) .Include(s => s.Statut) .Where(s => s.Statut.Label == Statuts.Accepte || s.Statut.Label == Statuts.Attente) .Include(t => t.CongeType) .Where(t => t.CongeType.Label == Types.Conge) .AsNoTracking() .ToListAsync(); var congesOwner = await _context.Conges .Where(c => c.CollaborateurID == currentUserId) .Include(s => s.Statut) .Where(s => s.Statut.Label == Statuts.Accepte || s.Statut.Label == Statuts.Attente) .AsNoTracking() .ToListAsync(); conges.Add(conge); foreach (var c in congesOwner) { if (c.DateDebut <= conge.DateFin && c.DateFin >= conge.DateDebut) { ViewData["Error"] = "Vous avez déjà une demande de congé durant cette période !"; return(View(viewModel)); } } var typeConge = await _context.CongeTypes.SingleAsync(t => t.Label == Types.Conge); foreach (var c in conges) { for (DateTime date = c.DateDebut; date <= c.DateFin; date = date.AddDays(1)) { if ((date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday) && (yearQuota.ContainsKey(date.Year))) { yearQuota[date.Year] = yearQuota[date.Year] + 1; if (yearQuota[date.Year] > 22 && c.CongeTypeID == typeConge.CongeTypeID) { ViewData["Error"] = "Vous dépassez votre quota pour l'année " + date.Year + " !"; return(View(viewModel)); } } } } try { conge.CollaborateurID = currentUserId; conge.StatutID = _context.Statuts .Single(s => s.Label == Statuts.Attente) .StatutID; conge.EnvoiDate = DateTime.Now; if (ModelState.IsValid) { _context.Add(conge); await _context.SaveChangesAsync(); return(RedirectToAction("Show")); } } catch (DbUpdateException ex) { ModelState.AddModelError("", "Unable to save changes. " + "Try again, and if the problem persists " + "see your system administrator."); } return(View(viewModel)); }