public async Task <IActionResult> Index(RegisterViewModel model, string returnUrl = null) { ViewData["ReturnUrl"] = returnUrl; if (await _activationInformant.IsRegistered()) { return(RedirectToLocal(returnUrl)); } if (ModelState.IsValid) { var company = new Company { Name = model.CompanyName, NbEmployees = model.NumberOfEmployee, Mail = model.Email, Address = model.AddressNumber + " " + model.AddressStreet + ", " + model.AddressBox + ", " + model.AddressPostalCode + ", " + model.AddressCity + ", " + model.AddressCountry, Status = false }; var manager = new Employee { UserName = model.UserName, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, Wallet = (decimal)0.00, Company = company }; RemoteCall remoteCaller = RemoteCall.GetInstance(); CommWrap <Company> responseCompany = await remoteCaller.RegisterCompany(company); if (responseCompany.RequestStatus == 1) { company.Id = responseCompany.Content.Id; company.ChkCode = responseCompany.Content.ChkCode; var result = await _userManager.CreateAsync(manager, model.Password); if (result.Succeeded) { IdentityRole role = new IdentityRole { Name = "Responsable", NormalizedName = "RESPONSABLE" }; bool roleExist = await _roleManager.RoleExistsAsync(role.NormalizedName); if (!roleExist) { IdentityResult roleResult = await _roleManager.CreateAsync(role); if (roleResult.Succeeded) { await _userManager.AddToRoleAsync(manager, role.Name); } } else { await _userManager.AddToRoleAsync(manager, role.Name); } return(RedirectToLocal(returnUrl)); } AddErrors(result); } AddErrors("Registration failed on the Snack side"); } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <IActionResult> DeleteConfirmed(int id) { Order order = await _context.Orders .Include(ord => ord.OrderLines) .ThenInclude(ordlin => ordlin.Sandwich) .Include(ord => ord.OrderLines) .ThenInclude(ordLin => ordLin.OrderLineVegetables) .ThenInclude(ordLinVeg => ordLinVeg.Vegetable) .SingleOrDefaultAsync(m => m.Id == id); OrderCompany_Com orderCompCom = new OrderCompany_Com(); orderCompCom.Order_com = order; orderCompCom.Company_com = _context.Companies.First(); int rep = await RemoteCall.GetInstance().SendOrderForDelete(orderCompCom); if (rep == 1) { foreach (OrderLine orderLine in order.OrderLines) { foreach (OrderLineVegetable ordLinVeg in orderLine.OrderLineVegetables) { _context.OrderLineVegetables.Remove(ordLinVeg); } _context.OrderLines.Remove(orderLine); } _context.Orders.Remove(order); string employeeId = _userManager.GetUserId(User); Employee employee = await _context.Employees.SingleOrDefaultAsync(m => m.Id == employeeId); employee.Wallet += order.TotalAmount; _context.Update(employee); await _context.SaveChangesAsync(); } return(RedirectToAction("Index")); }
public async Task <IActionResult> Index() { string id = _userManager.GetUserId(User); if (id == null) { return(NotFound()); } //Permet de recuperer un employer ainsi que toutes c'est commandes avec toutes les info ( sandwich , cruditees). Employee employee = await _context.Employees .Include(emp => emp.Orders) .ThenInclude(order => order.OrderLines) .ThenInclude(odLin => odLin.Sandwich) .Include(emp => emp.Orders) .ThenInclude(order => order.OrderLines) .ThenInclude(odLin => odLin.OrderLineVegetables) .ThenInclude(odLinVeg => odLinVeg.Vegetable) .SingleOrDefaultAsync(m => m.Id == id); if (employee == null) { return(NotFound()); } //Permet de supprimer les commande dont l'on a pas besoin pour l'affichage des commande du jour ou du lendemain si apres 10h DateTime now = DateTime.Now; DateTime delivreryDate; if (now.Hour >= 10) { delivreryDate = DateTime.Today.AddDays(1.0); } else { delivreryDate = DateTime.Today; } employee.Orders = employee.Orders.Where(q => q.DateOfDelivery.Equals(delivreryDate)).ToList(); //Permet de recupere les infos relatives aux sandwich et cruditées des commandes foreach (Order order in employee.Orders) { foreach (OrderLine orderline in order.OrderLines) { CommWrap <Sandwich> CommSan = await RemoteCall.GetInstance().GetSandwichById(orderline.Sandwich.Id); if (CommSan.RequestStatus == 1) { orderline.Sandwich = CommSan.Content; } else { return(NotFound()); } foreach (OrderLineVegetable orderLineVeg in orderline.OrderLineVegetables) { CommWrap <Vegetable> CommVeg = await RemoteCall.GetInstance().GetVegetableById(orderLineVeg.Vegetable.Id); if (CommVeg.RequestStatus == 1) { orderLineVeg.Vegetable = CommVeg.Content; } else { return(NotFound()); } } } } return(View(employee)); }
public async Task <IActionResult> Index() { RemoteCall remoteCaller = RemoteCall.GetInstance(); CommWrap <Menu> responseMenu = await remoteCaller.GetMenu(); MenuViewModel menuViewModel = new MenuViewModel(); if (responseMenu.RequestStatus == 1) { Menu menu = responseMenu.Content; menuViewModel.ListSandwiches = menu.Sandwiches.ToList(); menuViewModel.SelectedSandwich = null; menuViewModel.VegetablesPrice = menu.VegetablesPrice; List <VegWithChkBxViewModel> listVegWithChkBxViewModels = new List <VegWithChkBxViewModel>(); foreach (Vegetable veg in menu.Vegetables) { VegWithChkBxViewModel vegWithChkBxViewModel = new VegWithChkBxViewModel { Checked = false, Id = veg.Id, Name = veg.Name, Description = veg.Description }; listVegWithChkBxViewModels.Add(vegWithChkBxViewModel); } menuViewModel.ListVegetablesWithCheckBoxes = listVegWithChkBxViewModels; return(View("Index", menuViewModel)); } AddErrors("Le menu n'a pu être chargé correctement"); return(View("Index", menuViewModel)); /*string id = _userManager.GetUserId(User); * * if (id == null) * { * return NotFound(); * } * * var employee = await _context.Employees * .Include(emp => emp.Orders) * .ThenInclude(order => order.OrderLines) * .ThenInclude(orderLine => orderLine.Sandwich) * .Include(emp => emp.Orders) * .ThenInclude(order => order.OrderLines) * .ThenInclude(orderLine => orderLine.OrderLineVegetables) * .ThenInclude(orderLineVegetable => orderLineVegetable.Vegetable) * .SingleOrDefaultAsync(m => m.Id == id); * */ //string id = _userManager.GetUserId(User); //Employee emp = _context.Employees.Where(e => e.Id == id).FirstOrDefault(); //Order order = GetCurrentOrder(emp); //string serializable = HttpContext.Session.GetString("cart"); //Order order = JsonConvert.DeserializeObject<Order>(serializable); //return View(order.OrderLines.ToList()); }
public async Task <IActionResult> ValidateSessionCart() { Order cartOrder = ShoppingCart.GetCartContent(User, HttpContext); Company companyDb = _context.Companies.First(); //Si la company n'est visible en DB, c'est qui il y a un gros problème. if (companyDb == null) { return(NotFound());//Ou page d'erreur } CommWrap <OrderGuid_Com> comm = await RemoteCall.GetInstance().SendOrder(cartOrder, companyDb); if (comm.RequestStatus == 0) { //Si une erreur est survenue sur le serveur : company pas trouvée, erreur Db, ... return(NotFound());//Or pas d'erreur } Order orderUpdatedByServ = comm.Content.Order_com; string orderUpdatedGuid = comm.Content.Guid_com; string employeeId = _userManager.GetUserId(User); var employeeDb = await _context.Employees.SingleOrDefaultAsync(m => m.Id == employeeId); bool reussite = employeeDb.DebitWallet(orderUpdatedByServ.TotalAmount); if (reussite == false) { ModelState.AddModelError(string.Empty, "Porte monnaie insuffisant"); //L'employé n'a pas suffisement d'argent. CommWrap <string> commCancellation = await RemoteCall.GetInstance().ConfirmOrder(false, orderUpdatedGuid); return(await Index()); if (commCancellation.RequestStatus != 0) { //Il faut afficher à l'utilisateur qu'il n'a pas assez d'argent. //return } //L'annulation n'a pas aboutie, si on arrive ici, c'est que quelque //chose d'inattendu s'est produit ou peut être que la requête a été //appelée manuelle par l'utilisateur. //return } //L'employé avait suffisement de crédit et sa commande se confirme. CommWrap <string> commConfirmation = await RemoteCall.GetInstance().ConfirmOrder(true, orderUpdatedGuid); if (commConfirmation.RequestStatus != 0) { //Sauvegarder dans la DB coté client using (var tx = _context.Database.BeginTransaction(System.Data.IsolationLevel.Serializable)) { Order orderDb = await _context.Orders .Include(order => order.OrderLines) .ThenInclude(odLin => odLin.Sandwich) .Include(order => order.OrderLines) .ThenInclude(odLin => odLin.OrderLineVegetables) .ThenInclude(odLinVeg => odLinVeg.Vegetable) .SingleOrDefaultAsync(o => o.Employee.Equals(employeeDb) && o.DateOfDelivery.Equals(orderUpdatedByServ.DateOfDelivery)); if (orderDb == null) { //Si aucune commande pour cette journée n'a été trouvée, on lui assigne la nouvelle commande. for (int i = 0; i < orderUpdatedByServ.OrderLines.Count; i++) { Sandwich sandwichTemp = await _context.Sandwiches.SingleOrDefaultAsync(s => s.Id == orderUpdatedByServ.OrderLines.ElementAt(i).Sandwich.Id); if (sandwichTemp != null) { orderUpdatedByServ.OrderLines.ElementAt(i).Sandwich = sandwichTemp; } //_context.Entry(orderInValidation.OrderLines.ElementAt(i).Sandwich).State = EntityState.Unchanged; for (int j = 0; j < orderUpdatedByServ.OrderLines.ElementAt(i).OrderLineVegetables.Count; j++) { Vegetable vegetableTemp = await _context.Vegetables.SingleOrDefaultAsync(s => s.Id == orderUpdatedByServ.OrderLines.ElementAt(i).OrderLineVegetables.ElementAt(j).Vegetable.Id); if (vegetableTemp != null) { orderUpdatedByServ.OrderLines.ElementAt(i).OrderLineVegetables.ElementAt(j).Vegetable = vegetableTemp; } //_context.Entry(orderInValidation.OrderLines.ElementAt(i).OrderLineVegetables.ElementAt(j).Vegetable).State = EntityState.Unchanged; } } employeeDb.Orders.Add(orderUpdatedByServ); } else { //Une commande à été trouvé pour la journée, on somme celle-ci avec la nouvelle. orderDb.SumUpOrders(orderUpdatedByServ); } _context.SaveChanges(); tx.Commit(); } //Tout s'est bien passé, on peut vider le panier ShoppingCart.UpdateCartContent(HttpContext, null); return(RedirectToAction("Index")); } //La confirmation n'a pas aboutie, si on arrive ici, c'est que quelque //chose d'inattendu s'est produit ou peut être que la requête a été //appelée manuelle par l'utilisateur. //return return(RedirectToAction("Index"));//temporaire juste le temps des tests }