public async Task <bool> ExecutedAsync(string email) { try { var user = await db.Users.FirstOrDefaultAsync(x => x.Username == email); if (user == null) { return(false); } user.Verification = new Random().Next(100000, 1000000).ToString(); user.ExprireTime = DateTime.Now.AddMinutes(10); string body = File.ReadAllText("./Templates/MailVerificationTemplate.html"); body = body.Replace("@Name", user.Name); body = body.Replace("@Code", user.Verification); body = body.Replace("@Time", user.ExprireTime.GetValueOrDefault().ToString("dd/MM/yyyy HH:mm")); await sendMail.ExecutedAsync(user.Username, "Verification forget password", body); await db.SaveChangesAsync(); return(true); } catch (Exception ex) { return(false); } }
public async Task <bool> ExecutedAsync(RegisterViewModel registerViewModel) { try { if (await db.Users.AnyAsync(x => x.Username == registerViewModel.Email)) { return(false); } string verificationCode = verification.Random(); string body = File.ReadAllText("./Templates/MailConfirmTemplate.html"); body = body.Replace("@Name", registerViewModel.FullName); body = body.Replace("@Link", "http://localhost:4200/Activation/" + registerViewModel.Email + "/" + verificationCode); if (await sendMail.ExecutedAsync(registerViewModel.Email, "Account activation", body)) { string passwordEncrypt = encryption.EncryptionMD5(registerViewModel.Password); await db.Users.AddAsync(new Data.Entities.User() { Username = registerViewModel.Email, Name = registerViewModel.FullName, Image = Constants.DEFAUFT_IMAGE, Password = passwordEncrypt, Sex = registerViewModel.IsMale ? 0 : 1, ShipmentDetails = new List <ShipmentDetail> { new Data.Entities.ShipmentDetail() { Name = registerViewModel.FullName, Address = registerViewModel.Address, Email = registerViewModel.Email, PhoneNumber = registerViewModel.PhoneNumber, } }, Verification = verificationCode }); await db.SaveChangesAsync(); return(true); } return(false); } catch (Exception ex) { return(false); } }
public async Task <bool> ExecutedAsync(int userId, OrderCartViewModel orderCartViewModel) { try { Data.Entities.Order order = new Data.Entities.Order(); if (userId != 0) { order.CreatedBy = userId; orderCartViewModel.ShipmentDetail.UserId = userId; } if (await db.ShipmentDetails.AnyAsync(x => x.Id == orderCartViewModel.ShipmentDetail.Id)) { order.ShipmentDetailId = orderCartViewModel.ShipmentDetail.Id; } else { order.ShipmentDetail = orderCartViewModel.ShipmentDetail; } foreach (CartItemViewModel item in orderCartViewModel.CartItems) { if (await db.Products.Where(x => x.Id == item.Id).AnyAsync(x => x.Quantity.GetValueOrDefault() < orderCartViewModel.CartItems.Count)) { return(false); } order.OrderDetails.Add(new OrderDetail() { ProductId = item.Id, Count = item.Count, Price = item.Price, PromotionPrice = item.PromotionPrice }); } order.DeliveryMethod = orderCartViewModel.DeliveryMethod.Name; order.TotalShipping = orderCartViewModel.DeliveryMethod.Cost; order.PaymentMethod = orderCartViewModel.PaymentMethod; order.Ordered = DateTime.Now; order.Status = 1; db.Orders.Add(order); string body = File.ReadAllText("./Templates/MailOrderInformationTemplate.html"); body = body.Replace("@Name", orderCartViewModel.ShipmentDetail.Name); body = body.Replace("@Status", "được đặt"); body = body.Replace("@Time", DateTime.Now.ToString("dd/MM/yyyy HH:mm")); await sendMail.ExecutedAsync(orderCartViewModel.ShipmentDetail.Email, "Order information", body); await db.SaveChangesAsync(); return(true); } catch (Exception ex) { return(false); } }
public async Task <bool> ExecutedAsync(int orderId, bool isCancel) { try { var order = await db.Orders.Include(x => x.ShipmentDetail).Include(x => x.OrderDetails).Where(x => x.Id == orderId).FirstOrDefaultAsync(); if (order != null) { string body = File.ReadAllText("./Templates/MailOrderInformationTemplate.html"); body = body.Replace("@Name", order.User.Name); if (isCancel) { order.Status = 5; order.Canceled = DateTime.Now; body = body.Replace("@Status", "bị hủy"); body = body.Replace("@Time", DateTime.Now.ToString("dd/MM/yyyy HH:mm")); } else { switch (order.Status) { case 0: order.Ordered = DateTime.Now; order.Status++; body = body.Replace("@Status", "được đặt"); body = body.Replace("@Time", DateTime.Now.ToString("dd/MM/yyyy HH:mm")); break; case 1: order.Confirmed = DateTime.Now; order.Status++; body = body.Replace("@Status", "được xác nhận"); body = body.Replace("@Time", DateTime.Now.ToString("dd/MM/yyyy HH:mm")); order.OrderDetails.ToList().ForEach(x => x.Product.Quantity -= x.Count); break; case 2: order.Transporting = DateTime.Now; order.Status++; body = body.Replace("@Status", "chuyển sang trạng thái lấy hàng"); body = body.Replace("@Time", DateTime.Now.ToString("dd/MM/yyyy HH:mm")); break; case 3: order.Completed = DateTime.Now; order.Status++; body = body.Replace("@Status", "hoàn thành"); body = body.Replace("@Time", DateTime.Now.ToString("dd/MM/yyyy HH:mm")); break; } await sendMail.ExecutedAsync(order.ShipmentDetail.Email, "Order information", body); await db.SaveChangesAsync(); } } return(true); } catch (Exception ex) { return(false); } }