public void SendResetPasswordMail(string email) { using (var ctx = new AyerLechonContext()) { var account = ctx.Customers.FirstOrDefault(a => a.Email == email); if (account == null) { throw new ApplicationException("Email is not registered."); } ctx.Customers.Attach(account); account.ResetPasswordToken = Guid.NewGuid(); var resetPasswordUrl = ConfigurationManager.AppSettings["BaseUrl"] + "api/password/reset?token=" + account.ResetPasswordToken; var body = new StringBuilder(); body.AppendFormat("<p>Dear {0}, </p>", account.FirstName + " " + account.LastName); body.AppendFormat("<p>We received a request to change your password on <a href=\"{0}\">Ayer Lechon</a>. </p>", "http://ayerlechon.com"); body.Append("<p>Click the link below to set a new password: </p>"); body.AppendFormat("<h1><a href=\"{0}\">Reset Password</a></h1>", resetPasswordUrl); body.AppendFormat("<p>If you do not want to change your password you can ignore this email.</p>", "ResetPassword"); body.Append("<p>Thanks, </p>"); body.Append("<p>Ayer Lechon</p>"); var emailModel = new EmailViewModel() { Body = body.ToString(), EmailTo = email, Subject = "Password Reset" }; _emailService.Send(emailModel); ctx.SaveChanges(); } }
public void DummyItem() { using (var ctx = new AyerLechonContext()) { //you can change to uploaded file source var filepath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName + "\\Image\\image.jpg"; string mimeType = MimeMapping.GetMimeMapping(filepath); FileStream stream = File.OpenRead(filepath); byte[] fileBytes = new byte[stream.Length]; stream.Read(fileBytes, 0, fileBytes.Length); stream.Close(); //Begins the process of writing the byte array back to a file using (Stream file = File.OpenWrite(filepath)) { file.Write(fileBytes, 0, fileBytes.Length); } ctx.Items.Add(new Item { CategoryID = 1, Description = "Lechon Belly (49-70 pax)", FileStorage = new FileStorage { MIMEType = mimeType, FileName = Path.GetFileName(filepath), UploadedFile = fileBytes }, Price = 2520, ReadyStock = 10, }); ctx.Items.Add(new Item { CategoryID = 1, Description = "Lechon Belly (100-170 pax)", FileStorage = new FileStorage { MIMEType = mimeType, FileName = Path.GetFileName(filepath), UploadedFile = fileBytes }, Price = 3520, ReadyStock = 10, }); ctx.SaveChanges(); } //ctx.SaveChanges(); }
public HttpResponseMessage NewVIPApplication() { using (var ctx = new AyerLechonContext()) { ResponseViewModel <object> response; try { var userID = UserProvider.GetId(); var customer = ctx.Customers.FirstOrDefault(a => a.CustomerID == userID); if (customer == null) { response = new ResponseViewModel <object>() { Status = new Status() { Type = "Error", Message = "The customer is not found", }, Data = null }; return(Request.CreateResponse(HttpStatusCode.BadRequest, response)); } ctx.Customers.Attach(customer); customer.NewVIPApplication = true; ctx.SaveChanges(); response = new ResponseViewModel <object>() { Status = new Status() { Type = "Success", Message = "The customer has requested become vip member." }, Data = null }; return(Request.CreateResponse(HttpStatusCode.OK, response)); } catch (ApplicationException ae) { response = new ResponseViewModel <object>() { Status = new Status() { Type = "Error", Message = ae.Message, }, Data = null }; return(Request.CreateResponse(HttpStatusCode.BadRequest, response)); } } }
public void Create(Customer model) { using (var ctx = new AyerLechonContext()) { if (IsExist(model.Email)) { throw new ApplicationException("The email is already exist."); } ctx.Customers.Add(model); ctx.SaveChanges(); } }
public void ChangePassword(ChangePasswordViewModel model, int userId) { using (var ctx = new AyerLechonContext()) { var account = ctx.Customers.FirstOrDefault(a => a.CustomerID == userId && model.CurrentPassword == a.Password); if (account == null) { throw new ApplicationException("The current password is incorrect."); } ctx.Customers.Attach(account); account.Password = model.NewPassword; account.LastChangePassword = DateTimeOffset.Now.ToEpochTime(); ctx.SaveChanges(); } }
public void InitClientTable() { var ctx = new AyerLechonContext(); ctx.Clients.Add(new Client() { Active = true, AllowedOrigin = "*", ApplicationType = 0, Name = "IOS", RefreshTokenLifeTime = 1, Secret = "919d676f-fead-49eb-990c-b84848448df2", ClientID = Guid.Parse("EE8CF68C-BBA0-4615-A78D-683312CF03E3") }); ctx.SaveChanges(); }
public bool AddRefreshToken(RefreshToken token) { using (var ctx = new AyerLechonContext()) { var existingToken = ctx.RefreshTokens.FirstOrDefault(r => r.Subject == token.Subject && r.ClientId == token.ClientId); if (existingToken != null) { ctx.RefreshTokens.Remove(existingToken); } ctx.RefreshTokens.Add(token); return(ctx.SaveChanges() > 0); } }
public async Task <IHttpActionResult> RegisterExternal(RegisterExternalBindingModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var verifiedAccessToken = await VerifyExternalAccessToken(model.ExternalAccessToken); if (verifiedAccessToken == null) { return(BadRequest("Invalid Provider or External Access Token")); } using (var context = new AyerLechonContext()) { var customer = context.Customers.Include("LoginDevices").FirstOrDefault(a => a.Email == model.Email); if (customer == null) { customer = new Customer() { Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, VIP = false }; } var device = context.LoginDevices.FirstOrDefault(a => a.DeviceId == model.DeviceId); if (device != null) { context.LoginDevices.Remove(device); } var newdevice = new LoginDevice() { DeviceId = model.DeviceId, CreateDate = DateTime.Now.ToEpochTime(), FbAccountId = verifiedAccessToken.user_id, LastLoginDate = DateTime.Now.ToEpochTime(), }; customer.LoginDevices.Add(newdevice); context.SaveChanges(); var accessTokenResponse = GenerateLocalAccessTokenResponse(customer); return(Ok(accessTokenResponse)); } }
public void Update(ProfileViewModel model) { var customer = _context.Customers.FirstOrDefault(a => a.CustomerID == model.Id); if (customer.Email != model.Email) { var otherAccount = _context.Customers.FirstOrDefault(a => a.Email == model.Email); if (otherAccount != null) { throw new ApplicationException("The email is already exist"); } } _context.Customers.Attach(customer); customer.RegionID = model.RegionId; customer.PhoneNumber = model.PhoneNumber; customer.LastName = model.LastName; customer.FirstName = model.FirstName; customer.Email = model.Email; customer.Address = model.Address; _context.SaveChanges(); }
public void ResetPassword(string token) { using (var ctx = new AyerLechonContext()) { var tkn = Guid.Parse(token); var account = ctx.Customers.FirstOrDefault(a => a.ResetPasswordToken == tkn); if (account == null) { throw new ApplicationException("The token is expired. Please reset your password again."); } ctx.Customers.Attach(account); account.Password = RandomString(6); account.ResetPasswordToken = null; account.LastChangePassword = DateTimeOffset.Now.ToEpochTime(); var body = new StringBuilder(); body.AppendFormat("<p>Dear {0}, </p>", account.FirstName + " " + account.LastName); body.Append("<p>The password has been reset.</p>"); body.Append("<p>You can now log in with the following credentials:</p>"); body.AppendFormat("<p>Username: {0}</p>", account.Email); body.AppendFormat("<p>New Password: {0}</p>", account.Password); body.Append("<br/><br/><p>Thanks, </p>"); body.Append("<p>Ayer Lechon</p>"); var emailModel = new EmailViewModel() { Body = body.ToString(), EmailTo = account.Email, Subject = "Your new password" }; _emailService.Send(emailModel); ctx.SaveChanges(); } }
public void Create(OrderSummaryViewModel model) { var orderSummary = new OrderSummary() { AmountPaid = Convert.ToDecimal(model.Amount), CustomerID = model.CustomerId.Value, DeliveryAddress = model.DeliveryAddress, Notes = model.Notes, PaymentOptionId = model.PaymentOptionId.Value, PhoneNumber = model.PhoneNumber, RegionID = model.RegionId, DateNeeded = model.OrderDate, OrderDate = DateTime.Now.ToEpochTime() }; if (model.OrderDate < DateTime.Now.ToEpochTime()) { throw new ApplicationException("The Date Needed cannot be less than the current date."); } if (!string.IsNullOrEmpty(model.DiscountCode)) { var discount = _context.Discounts.FirstOrDefault(a => a.Code == model.DiscountCode); if (discount == null) { throw new ApplicationException("Voucher code is not found."); } var isUsed = _context.OrderSummaries.Any(a => a.CustomerID == model.CustomerId && a.DiscountId == discount.DiscountID); if (isUsed) { throw new ApplicationException("Voucher code has been used."); } orderSummary.DiscountId = discount.DiscountID; } foreach (var detail in model.OrderDetails) { var item = _context.Items.FirstOrDefault(a => a.ItemID == detail.ItemId); var orderDetail = new OrderDetail() { ItemID = detail.ItemId, SubTotal = detail.Quantity * item.Price, Quantity = detail.Quantity, Price = (double)item.Price }; orderSummary.OrderDetails.Add(orderDetail); orderItemService.DecreaseStock(orderDetail); } orderSummary.PaymentStatusId = PaymentStatusEnum.Unpaid; _context.OrderSummaries.Add(orderSummary); if (model.RegionId.HasValue) { UpdateRegion(model.CustomerId.Value, model.RegionId.Value); } PaymentOption(model.PaymentOptionId.Value, model.CustomerId.Value, model.Amount.Value); _context.SaveChanges(); }
public void DummyDiscount() { using (var ctx = new AyerLechonContext()) { var filepath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName + "\\Image\\promo.jpeg"; string mimeType = MimeMapping.GetMimeMapping(filepath); FileStream stream = File.OpenRead(filepath); byte[] fileBytes = new byte[stream.Length]; stream.Read(fileBytes, 0, fileBytes.Length); stream.Close(); //Begins the process of writing the byte array back to a file using (Stream file = File.OpenWrite(filepath)) { file.Write(fileBytes, 0, fileBytes.Length); } var expiredDate = DateTime.Now.AddMonths(1).ToEpochTime(); ctx.Discounts.Add(new Discount { Code = "TESTPROMO3", Description = "Example Promo 3", ExpiredDate = expiredDate, FileStorage = new FileStorage { MIMEType = mimeType, FileName = Path.GetFileName(filepath), UploadedFile = fileBytes }, }); ctx.Discounts.Add(new Discount { Code = "TESTPROMO4", Description = "Example Promo 4", ExpiredDate = expiredDate, FileStorage = new FileStorage { MIMEType = mimeType, FileName = Path.GetFileName(filepath), UploadedFile = fileBytes }, }); ctx.Discounts.Add(new Discount { Code = "TESTPROMO5", Description = "Example Promo 5", ExpiredDate = expiredDate, FileStorage = new FileStorage { MIMEType = mimeType, FileName = Path.GetFileName(filepath), UploadedFile = fileBytes }, }); ctx.SaveChanges(); } }