public async Task <ActionResult <FuelSupplier> > PostFuelSupplier(FuelSupplier fuelSupplier) { db.FuelSupplier.Add(fuelSupplier); await db.SaveChangesAsync(); return(CreatedAtAction("GetFuelSupplier", new { id = fuelSupplier.Id }, fuelSupplier)); }
// PUT: api/FuelSuppliers/5 public async Task <IActionResult> PutFuelSupplier(int id, FuelSupplier fuelSupplier) { if (id != fuelSupplier.Id) { return(BadRequest()); } db.Entry(fuelSupplier).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!FuelSupplierExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <ActionResult> InstallSupplierRoleAndUser() { // check if has at least one admin ApplicationRole role = await RoleManager.FindByNameAsync("Supplier"); if (role == null) { role = await CreateNewRole("Supplier"); } //City city = db.Cities.Where(s => s.Name_en == "city 1").FirstOrDefault(); if (role != null) { // create new user // check if [email protected] exist ApplicationUser userexist = await UserManager.FindByEmailAsync("*****@*****.**"); if (userexist == null) { var newuser = new ApplicationUser { UserName = "******", Email = "*****@*****.**", PhoneNumber = "xxxxxx", EmailConfirmed = true, }; var result = await UserManager.CreateAsync(newuser, "123456"); if (result.Succeeded) { await UserManager.AddToRoleAsync(newuser, "Supplier"); FuelSupplier fuelSupplier = new FuelSupplier() { Name = "Supplier", UserId = newuser.Id, IsDeleted = false, IsMiddler = false, CountryId = 73, }; db.Add(fuelSupplier); await db.SaveChangesAsync(); //await _signInManager.SignInAsync(newuser, isPersistent: false); TempData["Success"] = "تم إنشاء مدير النظام بنجاح... كلمة المرور 123456"; } } else { if (!(await UserManager.IsInRoleAsync(userexist, "Supplier"))) { await UserManager.AddToRoleAsync(userexist, "admin"); } } } return(RedirectToAction("Index")); }
public async Task <IActionResult> Buy(int?id) { var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); if (id == null) { return(NotFound()); } string userType = ""; if (User.IsInRole("Customer")) { userType = "Customer"; } if (User.IsInRole("Supplier")) { userType = "Supplier"; } var paymentPackage = await db.PaymentPackage .FirstOrDefaultAsync(m => m.Id == id); if (paymentPackage == null) { return(NotFound()); } ViewData["PaymentPackageId"] = (int)id; ViewData["PaymentPackage"] = paymentPackage; try { #region Stripe // Set your secret key. Remember to switch to your live secret key in production! // See your keys here: https://dashboard.stripe.com/account/apikeys StripeConfiguration.ApiKey = StripeOptions?.Value?.SecretKey; var options = new PaymentIntentCreateOptions { Amount = (int)(paymentPackage.Price * 100), Currency = paymentPackage.PriceUnit, // Verify your integration in this guide by including this parameter Metadata = new Dictionary <string, string>() { { "integration_check", "accept_a_payment" }, { "paymentPackageId", paymentPackage.Id.ToString() }, { "userId", userId }, { "userType", userType }, } }; var service = new PaymentIntentService(); var paymentIntent = service.Create(options); ViewBag.ClientSecret = paymentIntent.ClientSecret; #endregion } catch (Exception e) { Serilog.Log.Error(e, Helpers.Constants.PAYMENT_ERROR, $"User : {User.Identity.Name}"); Message = Toast.ErrorToastFront(GetExceptionMessage(e, "Payment Error. Contact site administrator.")); //return Content(e.ToString()); } if (User.IsInRole("Supplier")) { FuelSupplier fuelSupplier = db.FuelSupplier.Where(s => s.UserId == userId).FirstOrDefault(); ViewData["Supplier"] = fuelSupplier; return(View("BuyS")); } else { DBContext.Models.Customer customer = db.Customer.Where(c => c.UserId == userId).FirstOrDefault(); ViewData["Customer"] = customer; return(View("Buy")); } }
public async Task <IActionResult> Index() { ViewBag.what_we_offer = db.ContentManagement.Where(x => !x.IsDeleted).Where(a => a.Name == "what_we_offer").OrderBy(i => i.ItemOrder).ToList(); var cs = db.Users.ToList(); //cs.ForEach(async i => //{ // if(i.User != null) // { // var us = i.User; // await userManager.AddToRoleAsync(userManager.Users.First(x => x.Id == us.Id), "Customer"); // } //}); //var sps = db.FuelSupplier.ToList(); //sps.ForEach(async i => //{ // if(i.User != null) // { // var us = i.User; // await userManager.AddToRoleAsync(us, "Supplier"); // } //}); foreach (var user in cs) { if (user.Customer != null) { var us = user.Customer; await userManager.AddToRoleAsync(user, "Customer"); } else if (user.Customer == null && await userManager.IsInRoleAsync(user, "Customer")) { var c = new Customer() { CountryId = 233, ImageUrl = "/uploads/P51_10508883.jpg", FirstName = "customer" + cs.IndexOf(user), LastName = "customer" + cs.IndexOf(user), UserId = user.Id }; db.Add(c); db.SaveChanges(); } } foreach (var user in cs) { if (user.FuelSupplier != null) { await userManager.AddToRoleAsync(user, "Supplier"); } else if (user.FuelSupplier == null && await userManager.IsInRoleAsync(user, "Supplier")) { var c = new FuelSupplier() { CountryId = 233, ImageUrl = "/uploads/suppliers/AERO-Specialties-logo.jpg", Name = "Supplier" + cs.IndexOf(user), UserId = user.Id, IsMiddler = false }; db.Add(c); db.SaveChanges(); } } ; return(View()); }
public async Task <IActionResult> OnPostAsync() { ReturnUrl = Url.Content("~/"); if (ModelState.IsValid) { if (User.Identity.IsAuthenticated) { await _signInManager.SignOutAsync(); _logger.LogInformation("User logged out."); } var user = new ApplicationUser { UserName = Input.Email, Email = Input.Email }; var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); var UserManager = _serviceProvider.GetRequiredService <UserManager <ApplicationUser> >(); var RoleManager = _serviceProvider.GetRequiredService <RoleManager <ApplicationRole> >(); var roleResult = await RoleManager.FindByNameAsync("Supplier"); if (roleResult == null) { roleResult = new ApplicationRole("Supplier"); await RoleManager.CreateAsync(roleResult); } await UserManager.AddToRoleAsync(user, "Supplier"); FuelSupplier fuelSupplier = new FuelSupplier(); fuelSupplier.UserId = user.Id; fuelSupplier.Name = Input.Name; fuelSupplier.CountryId = Input.CountryId; fuelSupplier.IsMiddler = Input.IsMiddler; if (Input.file != null) { FileInfo fi = new FileInfo(Input.file.FileName); var newFilename = "P" + fuelSupplier.Id + "_" + string.Format("{0:d}", (DateTime.Now.Ticks / 10) % 100000000) + fi.Extension; var webPath = _hostingEnvironment.WebRootPath; var path = Path.Combine("", webPath + @"\uploads\suppliers\" + newFilename); var pathToSave = @"/uploads/suppliers/" + newFilename; using (var stream = new FileStream(path, FileMode.Create)) { await Input.file.CopyToAsync(stream); } fuelSupplier.ImageUrl = pathToSave; } SupplierContact supplierContact1 = new SupplierContact(); supplierContact1.ContactId = 3; supplierContact1.Value = Input.CompanyWebSite; SupplierContact supplierContact2 = new SupplierContact(); supplierContact2.ContactId = 18; supplierContact2.Value = _context.Country.Find(Input.CountryId) != null? _context.Country.Find(Input.CountryId).Name : ""; fuelSupplier.SupplierContact = new List <SupplierContact>(); fuelSupplier.SupplierContact.Add(supplierContact1); fuelSupplier.SupplierContact.Add(supplierContact2); SupplierContactPerson supplierContactPerson = new SupplierContactPerson(); supplierContactPerson.JobTitle = Input.Position; supplierContactPerson.Name = Input.Name; SupplierContactPersonContact supplierContactPersonContact = new SupplierContactPersonContact(); supplierContactPersonContact.ContactId = 7; supplierContactPersonContact.Value = Input.Email; supplierContactPerson.SupplierContactPersonContact = new List <SupplierContactPersonContact>(); supplierContactPerson.SupplierContactPersonContact.Add(supplierContactPersonContact); fuelSupplier.SupplierContactPerson = new List <SupplierContactPerson>(); fuelSupplier.SupplierContactPerson.Add(supplierContactPerson); _context.FuelSupplier.Add(fuelSupplier); _context.SaveChanges(); var contentAppName = _context.ContentManagement.Where(cm => cm.Name == "app_name") .FirstOrDefault(); string AppName = contentAppName == null ? "Fuel Services" : contentAppName.DisplayName; var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { userId = user.Id, code = code }, protocol: Request.Scheme); EmailBodyDefaultParams emailBodyDefaultParams = _context.EmailBodyDefaultParams .Where(e => e.EmailTypeName == "confirm_mail").FirstOrDefault(); string body = EmailSender.CreateEmailBody(emailBodyDefaultParams); body = body.Replace("{callbackurl}", HtmlEncoder.Default.Encode(callbackUrl)); var simpleResponse = EmailSender.SendEmail(Input.Email, AppName, body); TempData.Set("Toast", simpleResponse); return(LocalRedirect(ReturnUrl)); } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return(Page()); }
public async Task <IActionResult> Create(OfferViewModel model) { //var Continents = base.db.Continent.ToList(); //var Countries = db.Country.Where(d => !d.IsDeleted).Where(q => q.ContinentId == Continents.FirstOrDefault().Id).ToList(); //var Cities = db.City.Where(d => !d.IsDeleted).Where(q => q.CountryId == Countries.FirstOrDefault().Id).ToList(); //ViewData["ContinentId"] = new SelectList(Continents, "Id", "Name"); //ViewData["CountryId"] = new SelectList(Countries, "Id", "Name"); //ViewData["CityId"] = new SelectList(Cities, "Id", "Name"); ViewData["AirportId"] = new SelectList(base.db.Airport.Take(20), "Id", "Name"); ViewBag.FuelTypes = db.FuelType.ToList(); try { if (ModelState.IsValid) { if (model.AirportOffers.Count == 0) { ModelState.AddModelError("", "Select at least one airport."); throw new Exception(); } //checkDublication(model); var userId = (await GetCurrentUserAsync()).Id; //string userId = User.Identity.GetUserId(); FuelSupplier fuelSupplier = db.FuelSupplier.Where(d => !d.IsDeleted).Where(e => e.UserId == userId).FirstOrDefault(); if (fuelSupplier == null) { return(NotFound()); } int supplierId = fuelSupplier.Id; Offer offer = new Offer() { FuelSupplierId = supplierId, StartDate = model.StartDate, EndDate = model.EndDate, Status = OfferStatus.Active.ToString(), DuesTaxesLevies = model.DuesTaxesLevies, ItemOrder = model.ItemOrder, }; await base.db.Offer.AddAsync(offer); List <OfferFuelType> offerFuelType = new List <OfferFuelType>(); model.FuelTypes.ForEach( x => offerFuelType.Add(new OfferFuelType() { OfferId = offer.Id, FuelTypeId = x } )); await db.AddRangeAsync(offerFuelType); List <AirportOffer> AirportOffers = new List <AirportOffer>(); model.AirportOffers.ForEach( x => { Airport airport = db.Airport.Find(x.AirportId); var cityId = airport.CityId; var countryId = airport.CountryId; AirportOffer airportOffer = new AirportOffer() { AirportId = airport.Id, CityId = cityId, Price = x.Price, PriceUnit = x.PriceUnit, OfferId = offer.Id, }; AirportOffers.Add(airportOffer); } ); await db.AddRangeAsync(AirportOffers); await base.db.SaveChangesAsync(); Message = Toast.SucsessToast(); return(RedirectToAction("Details", new { offer.Id })); } model.AirportOffers.ForEach(x => x.Airport = db.Airport.Find(x.AirportId)); return(View(model)); } catch (Exception e) { ViewData["AirportId"] = new SelectList(base.db.Airport.Take(20), "Id", "Name"); ModelState.AddModelError("", GetExceptionMessage(e)); Serilog.Log.Error(e.Message); model.AirportOffers.ForEach(x => x.Airport = db.Airport.Find(x.AirportId)); return(View(model)); } }
public async Task <JsonResult> SupplierRegister([FromBody] SupplierRegisterModel model) { try { if (ModelState.IsValid) { if (User.Identity.IsAuthenticated) { await SignInManager.SignOutAsync(); Serilog.Log.Information("User logged out."); } var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { Serilog.Log.Information("User created a new account with password."); var roleResult = await RoleManager.FindByNameAsync("Supplier"); if (roleResult == null) { roleResult = new ApplicationRole() { Name = "Supplier" }; await RoleManager.CreateAsync(roleResult); } await UserManager.AddToRoleAsync(user, "Supplier"); FuelSupplier fuelSupplier = new FuelSupplier(); fuelSupplier.UserId = user.Id; fuelSupplier.Name = model.Name; fuelSupplier.CountryId = model.CountryId; fuelSupplier.IsMiddler = model.IsMiddler; if (model.file != null) { FileInfo fi = new FileInfo(model.file.FileName); var newFilename = "P" + fuelSupplier.Id + "_" + string.Format("{0:d}", (DateTime.Now.Ticks / 10) % 100000000) + fi.Extension; var webPath = _hostingEnvironment.WebRootPath; var path = Path.Combine("", webPath + @"\uploads\suppliers\" + newFilename); var pathToSave = @"/uploads/suppliers/" + newFilename; using (var stream = new FileStream(path, FileMode.Create)) { await model.file.CopyToAsync(stream); } fuelSupplier.ImageUrl = pathToSave; } SupplierContact supplierContact1 = new SupplierContact(); supplierContact1.ContactId = 3; supplierContact1.Value = model.CompanyWebSite; SupplierContact supplierContact2 = new SupplierContact(); supplierContact2.ContactId = 18; supplierContact2.Value = db.Country.Find(model.CountryId) != null? db.Country.Find(model.CountryId).Name : ""; fuelSupplier.SupplierContact = new List <SupplierContact>(); fuelSupplier.SupplierContact.Add(supplierContact1); fuelSupplier.SupplierContact.Add(supplierContact2); SupplierContactPerson supplierContactPerson = new SupplierContactPerson(); supplierContactPerson.JobTitle = model.Position; supplierContactPerson.Name = model.Name; SupplierContactPersonContact supplierContactPersonContact = new SupplierContactPersonContact(); supplierContactPersonContact.ContactId = 7; supplierContactPersonContact.Value = model.Email; supplierContactPerson.SupplierContactPersonContact = new List <SupplierContactPersonContact>(); supplierContactPerson.SupplierContactPersonContact.Add(supplierContactPersonContact); fuelSupplier.SupplierContactPerson = new List <SupplierContactPerson>(); fuelSupplier.SupplierContactPerson.Add(supplierContactPerson); db.FuelSupplier.Add(fuelSupplier); db.SaveChanges(); var contentAppName = db.ContentManagement.Where(cm => cm.Name == "app_name") .FirstOrDefault(); string AppName = contentAppName == null ? "Fuel Services" : contentAppName.DisplayName; var token = await UserManager.GenerateEmailConfirmationTokenAsync(user); byte[] tokenGeneratedBytes = Encoding.UTF8.GetBytes(token); var code = WebEncoders.Base64UrlEncode(tokenGeneratedBytes); var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { userId = user.Id, code = code }, protocol: Request.Scheme); EmailBodyDefaultParams emailBodyDefaultParams = db.EmailBodyDefaultParams .Where(e => e.EmailTypeName == "confirm_mail").FirstOrDefault(); string body = EmailSender.CreateEmailBody(emailBodyDefaultParams); body = body.Replace("{callbackurl}", HtmlEncoder.Default.Encode(callbackUrl)); var simpleResponse = EmailSender.SendEmail(model.Email, AppName, body); //var token = GetTokenForUser(user); return(new JsonResult(new Response <bool>(Constants.SUCCESS_CODE, true, simpleResponse.Message))); } else { string errors = ""; foreach (var error in result.Errors) { errors += error; } Serilog.Log.Error("Register Supplier", model.Email, errors); } } else { return(new JsonResult(new Response <bool>(Constants.INVALID_INPUT_CODE, false, Constants.INVALID_INPUT))); } } catch (Exception e) { Serilog.Log.Error(e, Constants.LogTemplates.LOGIN_ERROR_EX, model.Email); return(new JsonResult(new Response <bool>(Constants.SOMETHING_WRONG_CODE, false, GetExceptionMessage(e)))); } return(new JsonResult(new Response <bool>(Constants.SOMETHING_WRONG_CODE, false, Constants.SOMETHING_WRONG))); }