public async Task <int> SendOrderForDelete(OrderCompany_Com orderCompCom) { int i = -1; HttpResponseMessage response = await this.client.PostAsJsonAsync("api/Order/Supp", orderCompCom); if (response.IsSuccessStatusCode) { i = await response.Content.ReadAsAsync <int>(); } return(i); }
public async Task <CommWrap <OrderGuid_Com> > SendOrder(Order orderToSend, Company company) { CommWrap <OrderGuid_Com> responseReturn = null; OrderCompany_Com orderCompany = new OrderCompany_Com { Order_com = orderToSend, Company_com = company }; HttpResponseMessage response = await this.client.PostAsJsonAsync("api/Order", orderCompany); if (response.IsSuccessStatusCode) { responseReturn = await response.Content.ReadAsAsync <CommWrap <OrderGuid_Com> >(); } return(responseReturn); }
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 <CommWrap <OrderGuid_Com> > Post([FromBody] OrderCompany_Com communication) { Order orderClient = communication.Order_com; Company company = communication.Company_com; Company companyDB = null; List <Company> companyList = _context.Companies.Where(q => q.Id == company.Id && q.ChkCode == company.ChkCode).ToList(); //Si la company dans l'employé prétend faire partie n'est pas trouvée, s'arrêter. if (companyList.Count == 0) { return(new CommWrap <OrderGuid_Com> { RequestStatus = 0 }); } else { companyDB = companyList.First(); //Déterminer la date de la commande (seule la date server-side fait foi) DateTime now = DateTime.Now; DateTime deliveryDate; if (now.Hour >= 10) { deliveryDate = DateTime.Today.AddDays(1.0); } else { deliveryDate = DateTime.Today; } orderClient.DateOfDelivery = deliveryDate; //Rafraichir les anciennes données avec des nouvelles par mesure de sécurité (les commandes //aux alentours de 10h sont susceptibles d'être altérées) using (var tx = _context.Database.BeginTransaction(System.Data.IsolationLevel.Serializable)) { try { for (int i = 0; i < orderClient.OrderLines.Count; i++) { orderClient.OrderLines.ElementAt(i).Sandwich.Price = _context.Sandwiches.Where(s => s.Id == orderClient.OrderLines.ElementAt(i).Sandwich.Id).First().Price; orderClient.OrderLines.ElementAt(i).Sandwich.Name = _context.Sandwiches.Where(s => s.Id == orderClient.OrderLines.ElementAt(i).Sandwich.Id).First().Name; orderClient.OrderLines.ElementAt(i).Sandwich.Description = _context.Sandwiches.Where(s => s.Id == orderClient.OrderLines.ElementAt(i).Sandwich.Id).First().Description; for (int j = 0; j < orderClient.OrderLines.ElementAt(i).OrderLineVegetables.Count; j++) { //On en aura besoin plus tard pour le calcul du prix (ce n'est pas sauvegardé dans la DB). orderClient.OrderLines.ElementAt(i).VegetablesPrice = _context.Menus.First().VegetablesPrice; orderClient.OrderLines.ElementAt(i).OrderLineVegetables.ElementAt(j).Vegetable.Name = _context.Vegetables.Where(v => v.Id == orderClient.OrderLines.ElementAt(i).OrderLineVegetables.ElementAt(j).Vegetable.Id).First().Name; orderClient.OrderLines.ElementAt(i).OrderLineVegetables.ElementAt(j).Vegetable.Description = _context.Vegetables.Where(v => v.Id == orderClient.OrderLines.ElementAt(i).OrderLineVegetables.ElementAt(j).Vegetable.Id).First().Description; } } } catch (Exception e) { return(new CommWrap <OrderGuid_Com> { RequestStatus = 0 }); } tx.Commit(); } //Mettre à jour le prix de la commande avec les nouvelles données. orderClient.UpdateTotalAmount(); string orderTemporaryCode = Guid.NewGuid().ToString(); ordersToValidate.Add(orderTemporaryCode, communication); OrderGuid_Com response = new OrderGuid_Com { Guid_com = orderTemporaryCode, Order_com = communication.Order_com }; return(new CommWrap <OrderGuid_Com> { RequestStatus = 1, Content = response }); } }
public async Task <int> PostSupp([FromBody] OrderCompany_Com communication) { DateTime now = DateTime.Now; DateTime delivreryDate; if (now.Hour >= 10) { delivreryDate = DateTime.Today.AddDays(1.0); } else { delivreryDate = DateTime.Today; } if (delivreryDate.Equals(communication.Order_com.DateOfDelivery)) { using (var transaction = _context.Database.BeginTransaction(System.Data.IsolationLevel.Serializable)) { try { Company companyDb = await _context.Companies.SingleOrDefaultAsync(c => c.Id == communication.Company_com.Id); Order order = await _context.Orders .Include(orderQ => orderQ.OrderLines) .ThenInclude(odLin => odLin.Sandwich) .Include(orderQ => orderQ.OrderLines) .ThenInclude(odLin => odLin.OrderLineVegetables) .ThenInclude(odLinVeg => odLinVeg.Vegetable) .SingleOrDefaultAsync(o => o.Company.Equals(companyDb) && o.DateOfDelivery.Equals(delivreryDate)); int orderLineSupp = 0; for (int i = 0; i < communication.Order_com.OrderLines.Count; ++i) { int j; for (j = 0; j < order.OrderLines.Count && !order.OrderLines.ElementAt(j).Equals(communication.Order_com.OrderLines.ElementAt(i)); ++j) { ; } order.TotalAmount -= communication.Order_com.OrderLines.ElementAt(i).Quantity *order.OrderLines.ElementAt(j).GetPrice() / order.OrderLines.ElementAt(j).Quantity; order.OrderLines.ElementAt(j).Quantity -= communication.Order_com.OrderLines.ElementAt(i).Quantity; if (order.OrderLines.ElementAt(j).Quantity == 0) { _context.OrderLines.Attach(order.OrderLines.ElementAt(j)); _context.OrderLines.Remove(order.OrderLines.ElementAt(j)); ++orderLineSupp; } else { _context.OrderLines.Attach(order.OrderLines.ElementAt(j)); var entry = _context.Entry(order.OrderLines.ElementAt(j)); entry.Property(e => e.Quantity).IsModified = true; } } if (order.OrderLines.Count == orderLineSupp) { _context.Orders.Attach(order); _context.Orders.Remove(order); } else { _context.Orders.Attach(order); var entryOrder = _context.Entry(order); entryOrder.Property(e => e.TotalAmount).IsModified = true; } _context.SaveChanges(); transaction.Commit(); return(1); } catch (Exception) { transaction.Rollback(); return(0); } } } else { return(0); } }
//[Route("api/Order/Confirm")] public async Task <CommWrap <string> > Confirm([FromBody] CommWrap <string> communication) { //Si le client indique un echec de son coté (pas assez de crédit par exemple) if (communication.RequestStatus == 0) { if (ordersToValidate.ContainsKey(communication.Content)) { //Supprimer l'Order de la liste en attente ordersToValidate.Remove(communication.Content); return(new CommWrap <string> { RequestStatus = 0 }); } } //Si il n'y a pas de eu de soucis chez le client, on continue if (!ordersToValidate.ContainsKey(communication.Content)) { //Si on ne trouve pas la commande que le client veut valider return(new CommWrap <string> { RequestStatus = 0 }); } else { OrderCompany_Com orderInValidationInfo = ordersToValidate[communication.Content]; ordersToValidate.Remove(communication.Content); //////////// //Récolte des infos Db nécessaire pour enregistrer la commande. //////////// Company company = orderInValidationInfo.Company_com; Order orderInValidation = orderInValidationInfo.Order_com; using (var tx = _context.Database.BeginTransaction(System.Data.IsolationLevel.Serializable)) { Company companyDb = await _context.Companies.SingleOrDefaultAsync(c => c.Id == company.Id && c.ChkCode == company.ChkCode); 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.Company.Equals(companyDb) && o.DateOfDelivery.Equals(orderInValidation.DateOfDelivery)); if (companyDb == null) { //Si, étrangement, on n'a pas trouvé la company (mesure de sécurité). return(new CommWrap <string> { RequestStatus = 0 }); } for (int i = 0; i < orderInValidation.OrderLines.Count; i++) { Sandwich sandwichTemp = await _context.Sandwiches.SingleOrDefaultAsync(s => s.Id == orderInValidation.OrderLines.ElementAt(i).Sandwich.Id); orderInValidation.OrderLines.ElementAt(i).Sandwich = sandwichTemp; //_context.Entry(orderInValidation.OrderLines.ElementAt(i).Sandwich).State = EntityState.Unchanged; for (int j = 0; j < orderInValidation.OrderLines.ElementAt(i).OrderLineVegetables.Count; j++) { Vegetable vegetableTemp = await _context.Vegetables.SingleOrDefaultAsync(s => s.Id == orderInValidation.OrderLines.ElementAt(i).OrderLineVegetables.ElementAt(j).Vegetable.Id); orderInValidation.OrderLines.ElementAt(i).OrderLineVegetables.ElementAt(j).Vegetable = vegetableTemp; //_context.Entry(orderInValidation.OrderLines.ElementAt(i).OrderLineVegetables.ElementAt(j).Vegetable).State = EntityState.Unchanged; } } if (orderDb == null) { //Si aucune commande pour cette journée n'a été trouvée, on lui assigne la nouvelle commande. companyDb.Orders.Add(orderInValidation); } else { //Une commande à été trouvé pour la journée, on somme celle-ci avec la nouvelle. orderDb.SumUpOrders(orderInValidation); } _context.SaveChanges(); tx.Commit(); } return(new CommWrap <string> { RequestStatus = 1, Content = "" }); } }