public Cookies() { //instantiate services var optionsBuilder = new DbContextOptionsBuilder(); var useCosmos = $"{ConfigurationManager.AppSettings["UseCosmos"]}".ToUpper() == "TRUE"; if (useCosmos) { var accountEndpoint = ConfigurationManager.AppSettings["CosmosAccountEndpoint"]; var databaseName = ConfigurationManager.AppSettings["CosmosDatabaseName"]; var authKeyName = ConfigurationManager.AppSettings["CosmosAccountKeyName"]; var accountKey = Environment.GetEnvironmentVariable(authKeyName); optionsBuilder = optionsBuilder.UseCosmos(accountEndpoint, accountKey, databaseName); } else { optionsBuilder = optionsBuilder.UseSqlServer(ConfigurationManager.ConnectionStrings["CookieDBConnection"].ConnectionString); } var context = new CookieContext(optionsBuilder.Options); context.EnsureCreatedAndSeedAsync().Wait(); _cookieService = new CookieService(context); _orderService = new OrderService(context); }
public void PlaceOrder(int orderId, Guid storeId) { CookieContext context = _contextFactory.Context(storeId); if (orderId == 0) //just to check that this is the new order with id 0 { var newOrder = _cache.GetString(storeId.ToString()); if (!string.IsNullOrEmpty(newOrder)) { var order = JsonConvert.DeserializeObject <Order>(newOrder); foreach (var line in order.OrderLines) { //have to get a reference to the actual cookie, not the attached one, otherwise EF will protest line.Cookie = context.Cookies.Where(c => c.Id == line.Cookie.Id).FirstOrDefault(); } order.Status = "Placed"; //have to get a reference to the actual store, not the attached one, otherwise EF will protest order.Store = context.Stores.Where(s => s.Id == order.Store.Id).FirstOrDefault(); context.Orders.Add(order); if (context.SaveChanges() > 0) //check for success { //if all went well, remove the item from cache _cache.Remove(storeId.ToString()); } } } }
public UserService(CookieContext context, IDataHashManager hashManager, IJwtHandler jwtHandler, IRecipeService recipeService) { _context = context; _hashManager = hashManager; _jwtHandler = jwtHandler; _recipeService = recipeService; }
public RoutingService(IAccountService accountService, IOptions <ConnectionStrings> connectionStrings, IDistributedCache cache, CookieContext context) { _accountService = accountService; _connectionStrings = connectionStrings.Value; _cache = cache; _currentContext = context; }
public void AddCookieToOrder(int cookieId, Guid storeId) { CookieContext context = _contextFactory.Context(storeId); var orderString = _cache.GetString(storeId.ToString()); Order storesOrder; if (!string.IsNullOrEmpty(orderString)) { storesOrder = JsonConvert.DeserializeObject <Order>(orderString); bool orderLineExists = false; foreach (var lines in storesOrder.OrderLines) { if (lines.Cookie.Id == cookieId) { lines.Quantity++; orderLineExists = true; storesOrder.Price += lines.Cookie.Price; } } if (!orderLineExists) { var cookie = context.Cookies.Where(c => c.Id == cookieId).FirstOrDefault(); storesOrder.OrderLines.Add(new OrderLine { Cookie = cookie, Quantity = 1 }); storesOrder.Price += cookie.Price; } } else { storesOrder = new Order(); storesOrder.Date = DateTimeOffset.Now; storesOrder.Store = _storeService.GetStoreById(storeId); storesOrder.Status = "New"; var cookie = context.Cookies.Where(c => c.Id == cookieId).FirstOrDefault(); storesOrder.OrderLines.Add(new OrderLine { Cookie = cookie, Quantity = 1 }); storesOrder.Price += cookie.Price; } _cache.SetString(storeId.ToString(), JsonConvert.SerializeObject(storesOrder)); }
public Orders() { //instantiate services var optionsBuilder = new DbContextOptionsBuilder() .UseSqlServer(ConfigurationManager.ConnectionStrings["CookieDBConnection"].ConnectionString); var context = new CookieContext(optionsBuilder.Options); _orderService = new OrderService(context); }
public void SyncStoresToDatabases() { var storesToSync = _currentContext.Stores.ToList(); var servers = _currentContext.DatabaseServers.ToList(); foreach (var server in servers) { var optionsBuilder = new DbContextOptionsBuilder(); optionsBuilder.UseSqlServer(GetDataStoreForDatabaseServer(server)); var context = new CookieContext(optionsBuilder.Options); try { var stores = context.Stores.ToList(); //loop over the stores to sync foreach (var store in storesToSync) { //if the store is not in the current context's stores list if (stores.Where(s => s.Name == store.Name).FirstOrDefault() == null) { //build a new storeobject, so that the connection to the old DBContext is gone Store storeToAdd = new Store { Name = store.Name, Country = store.Country, DatabaseServer = context.DatabaseServers.Where(d => d.Id == store.DatabaseServer.Id).FirstOrDefault(), Orders = store.Orders }; context.Stores.Add(storeToAdd); context.SaveChanges(); } } } catch (SqlException ex) { //database cannot be openened for some reason } } }
public void CancelOrder(int orderId, Guid storeId) { CookieContext context = _contextFactory.Context(storeId); if (orderId == 0) { //cancel the order in cache _cache.Remove(storeId.ToString()); } else { //the order is in the database, remove it var order = context.Orders.Where(o => o.Id == orderId).FirstOrDefault(); if (order != null) { context.Remove(order); context.SaveChanges(); } } }
public AdminService(CookieContext context, IDataHashManager hashManager, IJwtHandler jwtHandler) { _context = context; _hashManager = hashManager; _jwtHandler = jwtHandler; }
public RecipeService(CookieContext context) { _context = context; }
public OrderService(IDistributedCache cache, IStoreService storeService, CookieContext context) { _cache = cache; _storeService = storeService; _context = context; }
public StoreService(CookieContext context) { _context = context; }
public OrderService(CookieContext context) { _context = context; }
public UserStatisticsService(CookieContext context) => _context = context;
public CookieService(CookieContext context) : this(context, null) { }
public CookieService(CookieContext context, IDistributedCache cache) { _context = context; _cache = cache; }
public AccountService(CookieContext context) { _context = context; }
public CategoryService(CookieContext context) { _context = context; }
public RecipeStatisticsService(CookieContext context) => _context = context;
public RateService(CookieContext context) { _context = context; }
public UserImageService(CookieContext context) { _context = context; }