public void LogError(string message, DateTime time, string requestUrl, string clientIp, string userAgent, string sessionId, Guid?userId) { try { using (var context = new InstantStoreDataContext()) { // Logging message first in case user data is broken. context.ErrorLogs.InsertOnSubmit(new ErrorLog() { Id = Guid.NewGuid(), ExceptionText = message, DateTime = time, UserId = userId, SessionId = sessionId, RequestUrl = requestUrl, ClientIP = clientIp, UserAgent = userAgent }); context.SubmitChanges(); } } catch (Exception ex) { string error = string.Format( "Error occurred during logging the error: exception info {0}. " + "Error data: innerException: {1}, time: {2}, requestUrl: {3}, clientIp: {4}, userAgent: {5}, sessionId: {6}.", ex.ToString(), time, requestUrl, clientIp, userAgent, sessionId); Trace.TraceError(ex.ToString()); } }
public void ChangePagePosition(Guid id, bool movedown) { using (var context = new InstantStoreDataContext()) { var page = context.ContentPages.FirstOrDefault(x => x.Id == id); if (page == null) { throw new ModelValidationException("UpdateContentPage.OriginalPageDoesNotExists"); } if (page.Position == 0 && !movedown) { return; } var siblingPages = context.ContentPages.Where(x => page.ParentId != null ? x.ParentId == page.ParentId : x.ParentId == null && x.Id != page.Id).ToList(); if (page.Position >= siblingPages.Count && movedown) { return; } int newPosition = page.Position + (movedown ? 1 : -1); var exchangePage = siblingPages.FirstOrDefault(x => x.Position == newPosition); if (exchangePage != null) { exchangePage.Position = page.Position; } page.Position = newPosition; context.SubmitChanges(); } }
public void TrashPage(Guid id) { using (var context = new InstantStoreDataContext()) { var trashPage = context.ContentPages.FirstOrDefault(x => x.Id == TrashParentId); if (trashPage == null) { context.ContentPages.InsertOnSubmit(new ContentPage() { Id = TrashParentId, Name = "TRASH", Position = -1 }); } var page = context.ContentPages.FirstOrDefault(x => x.Id == id); if (page == null) { throw new ModelValidationException("UpdateContentPage.OriginalPageDoesNotExists"); } page.ParentId = TrashParentId; context.SubmitChanges(); } }
public Order AddItemToCurrentOrder(User user, Guid productId, int count) { using (var context = new InstantStoreDataContext()) { var order = context.Orders.FirstOrDefault(x => x.UserId == user.Id && x.Status == (int)OrderStatus.Active); if (order == null) { order = this.CreateOrderForUser(context, user.Id); } if (user.DefaultCurrencyId == null || user.DefaultCurrencyId == Guid.Empty) { throw new ModelValidationException("User has no currency assigned."); } if (order.Status != (int)OrderStatus.Active) { throw new ModelValidationException("Order has already been submitted."); } AddOrUpdateOrderItem(user, productId, count, context, order); context.SubmitChanges(); return(order); } }
public void BlockUser(Guid userId) { using (var context = new InstantStoreDataContext()) { var user = context.Users.First(u => u.Id == userId); user.IsBlocked = true; context.SubmitChanges(); } }
public void DeleteTemplate(Guid id) { using (var context = new InstantStoreDataContext()) { var template = context.PropertyTemplates.FirstOrDefault(x => x.Id == id); context.PropertyTemplates.DeleteOnSubmit(template); context.SubmitChanges(); } }
public void ActivateUser(Guid userId) { using (var context = new InstantStoreDataContext()) { var user = context.Users.FirstOrDefault(u => u.Id == userId); user.IsActivated = true; context.SubmitChanges(); } }
public void ResetPassword(Guid userId, string newPassword) { using (var context = new InstantStoreDataContext()) { var user = context.Users.FirstOrDefault(u => u.Id == userId); user.Password = PasswordHash.PasswordHash.CreateHash(newPassword); context.SubmitChanges(); } }
public void AddFeedback(Feedback feedback) { using (var context = new InstantStoreDataContext()) { feedback.Id = Guid.NewGuid(); feedback.Submitted = DateTime.Now; context.Feedbacks.InsertOnSubmit(feedback); context.SubmitChanges(); } }
/// <summary> /// Updates groups for the given category. /// </summary> /// <remarks> /// Assumes that child groups are valid. /// </remarks> public static Guid?UpdateCategoryGropus(Guid categoryId, InstantStoreDataContext context) { var categoryPage = context.ContentPages.FirstOrDefault(x => x.Id == categoryId); if (categoryPage == null) { return(categoryId); } // If the page is not category then return. if (!categoryPage.IsCategory()) { return(categoryPage.ParentId); } // Extract all existing groups var categoryGroups = context.ProductToCategories.Where(x => x.CategoryId == categoryId).ToList(); var categoryChildren = context.ContentPages.Where(x => x.ParentId != null && x.ParentId == categoryId).ToList().Where(x => x.IsCategory()); // Extracting the category children. foreach (var childPage in categoryChildren) { // All products from the real child category var childProducts = context.ProductToCategories.Where(x => x.CategoryId == childPage.Id).ToList(); // Products of the child group of the current category. var childGroup = categoryGroups.Where(x => x.GroupId == childPage.Id).ToList(); // Delete all products which are not exist in the child anymore. var productsToDeleteInGroup = childGroup.Where(x => !childProducts.Any(y => y.ProductId == x.ProductId)).ToList(); context.ProductToCategories.DeleteAllOnSubmit(productsToDeleteInGroup); // Insert all products which are missing in the category group. var productsToInsertInGroup = childProducts.Where(x => !childGroup.Any(y => y.ProductId == x.ProductId)).ToList(); context.ProductToCategories.InsertAllOnSubmit(productsToInsertInGroup.Select(x => new ProductToCategory { Id = Guid.NewGuid(), CategoryId = categoryId, GroupId = childPage.Id, ProductId = x.ProductId, UpdateTime = DateTime.Now })); } // Removing groups which are no longer children var noChildGroup = categoryGroups.Where(x => x.GroupId != null && !categoryChildren.Any(y => y.Id == x.GroupId)); context.ProductToCategories.DeleteAllOnSubmit(noChildGroup); // Update all the gropus. context.SubmitChanges(); // Return category's parent id. return(categoryPage.ParentId); }
public ActionResult Order(OrderDetailsViewModel orderViewModel) { var user = UserIdentityManager.GetActiveUser(this.Request, repository); if (user == null || !user.IsAdmin || orderViewModel == null) { return(this.HttpNotFound()); } this.ViewData["MainMenuViewModel"] = MenuViewModelFactory.CreateAdminMenu(repository, ControlPanelPage.Orders); this.ViewData["SettingsViewModel"] = this.settingsViewModel; using (var context = new InstantStoreDataContext()) { var order = context.Orders.FirstOrDefault(x => x.Id == orderViewModel.Id); if (order == null) { return(this.HttpNotFound()); } if (order.Comment != orderViewModel.Description || order.Status != (int)orderViewModel.Status) { order.Comment = orderViewModel.Description; if (order.Status != (int)orderViewModel.Status) { order.Status = (int)orderViewModel.Status; context.OrderUpdates.InsertOnSubmit(new OrderUpdate { Status = (int)orderViewModel.Status, DateTime = DateTime.Now, Id = Guid.NewGuid(), OrderId = order.Id }); } var orderSubmitDate = order.OrderUpdates.FirstOrDefault(x => x.Status == (int)OrderStatus.Placed); context.SubmitChanges(); EmailManager.Send( order.User, this.repository, EmailType.EmailOrderHasBeenUpdated, new Dictionary <string, string> { { "%order.id%", order.Id.ToString() }, { "%order.user%", order.User.Name }, { "%order.date%", orderSubmitDate != null ? orderSubmitDate.DateTime.ToString("F", russianCulture) : string.Empty } }); } } return(this.RedirectToAction("Orders")); }
public Guid NewCategory(Category category) { using (var context = new InstantStoreDataContext()) { category.Id = Guid.NewGuid(); category.VersionId = Guid.NewGuid(); context.Categories.InsertOnSubmit(category); context.SubmitChanges(); return(category.VersionId); } }
public Guid AddAttachment(Attachment attachment) { using (var context = new InstantStoreDataContext()) { var id = Guid.NewGuid(); attachment.Id = id; context.Attachments.InsertOnSubmit(attachment); context.SubmitChanges(); return(id); } }
public Guid NewProduct(Product product) { using (var context = new InstantStoreDataContext()) { product.Id = Guid.NewGuid(); product.VersionId = Guid.NewGuid(); context.Products.InsertOnSubmit(product); context.SubmitChanges(); return(product.VersionId); } }
public void RemoveOrderProduct(Guid orderProductId) { using (var context = new InstantStoreDataContext()) { var orderProduct = context.OrderProducts.FirstOrDefault(x => x.Id == orderProductId); if (orderProduct != null) { context.OrderProducts.DeleteOnSubmit(orderProduct); context.SubmitChanges(); } } }
public Guid AddNewTemplate(PropertyTemplate propertyTemplate) { using (var context = new InstantStoreDataContext()) { var id = Guid.NewGuid(); propertyTemplate.Id = id; propertyTemplate.IsPrototype = true; context.PropertyTemplates.InsertOnSubmit(propertyTemplate); context.SubmitChanges(); return(id); } }
public Guid NewPage(ContentPage contentPage, IList <Guid> attachmentIds) { using (var context = new InstantStoreDataContext()) { contentPage.Id = Guid.NewGuid(); UpdateAttachmentName(contentPage, attachmentIds, context); context.ContentPages.InsertOnSubmit(contentPage); context.SubmitChanges(); return(contentPage.Id); } }
public void AddUser(User user) { using (var context = new InstantStoreDataContext()) { user.DefaultCurrencyId = user.DefaultCurrencyId == null?context.Currencies.FirstOrDefault().Id : user.DefaultCurrencyId; user.Id = Guid.NewGuid(); user.Password = PasswordHash.PasswordHash.CreateHash(user.Password); context.Users.InsertOnSubmit(user); context.SubmitChanges(); } }
public void UpdatePassword(Guid userId, string password) { using (var context = new InstantStoreDataContext()) { var user = context.Users.FirstOrDefault(u => u.Id == userId); if (user != null) { user.Password = PasswordHash.PasswordHash.CreateHash(password); context.SubmitChanges(); } } }
public void DeleteCurrency(Guid id) { using (var context = new InstantStoreDataContext()) { var currency = context.Currencies.FirstOrDefault(c => c.Id == id); if (currency != null) { context.Currencies.DeleteOnSubmit(currency); context.SubmitChanges(); } } }
public void DeleteExchangeRate(Guid id) { using (var context = new InstantStoreDataContext()) { var exchangeRate = context.ExchangeRates.FirstOrDefault(e => e.Id == id); if (exchangeRate != null) { context.ExchangeRates.DeleteOnSubmit(exchangeRate); context.SubmitChanges(); } } }
public void SubmitOrder(Guid orderId) { using (var context = new InstantStoreDataContext()) { var order = context.Orders.FirstOrDefault(x => x.Id == orderId); if (order != null) { if (!order.OrderProducts.Any()) { throw new ModelValidationException("NoProducts"); } if (order.User.DefaultCurrencyId == null) { throw new ModelValidationException("NoUserCurrencySet"); } Guid userCurrency = order.User.DefaultCurrencyId.Value; if (!order.OrderProducts.All(x => x.PriceCurrencyId == userCurrency)) { throw new ModelValidationException("ProductCurrencyIsInvalid"); } if (order.OrderProducts.Any(x => x.Price <= 0)) { throw new ModelValidationException("ProductPriceIsNotSet"); } order.Status = (int)OrderStatus.Placed; order.TotalPrice = order.OrderProducts.Sum(x => x.Price * x.Count); if (order.Offer != null) { order.TotalPrice = order.Offer.DiscountType == (int)OfferDiscountType.Percent ? order.TotalPrice * (1 - order.Offer.DiscountValue / 100.0M) : order.TotalPrice - order.Offer.DiscountValue; } order.PriceCurrencyId = userCurrency; context.OrderUpdates.InsertOnSubmit(new OrderUpdate { Id = Guid.NewGuid(), OrderId = orderId, Status = order.Status, DateTime = DateTime.Now }); context.SubmitChanges(); } } }
public void UpdateExchangeRate(ExchangeRate rate) { using (var context = new InstantStoreDataContext()) { var exchangeRate = context.ExchangeRates.FirstOrDefault(e => e.Id == rate.Id); if (exchangeRate != null) { exchangeRate.ConversionRate = rate.ConversionRate; exchangeRate.ReverseConversionRate = rate.ReverseConversionRate; context.SubmitChanges(); } } }
public void UpdateOrderProduct(OrderProduct orderProduct) { using (var context = new InstantStoreDataContext()) { var orderProductUpdated = context.OrderProducts.FirstOrDefault(x => x.Id == orderProduct.Id); if (orderProductUpdated != null) { orderProductUpdated.Count = orderProduct.Count; orderProductUpdated.Price = orderProduct.Price; orderProductUpdated.PriceCurrencyId = orderProduct.PriceCurrencyId; context.SubmitChanges(); } } }
public void AddCurrency(string text) { using (var context = new InstantStoreDataContext()) { var currency = new Currency { Id = Guid.NewGuid(), Text = text }; context.Currencies.InsertOnSubmit(currency); context.SubmitChanges(); } }
public void UpdateUser(User user) { using (var context = new InstantStoreDataContext()) { var userUpdated = context.Users.FirstOrDefault(u => u.Id == user.Id); userUpdated.Name = user.Name; userUpdated.City = user.City; userUpdated.Company = user.Company; userUpdated.DefaultCurrencyId = user.DefaultCurrencyId != Guid.Empty ? user.DefaultCurrencyId : null; userUpdated.Email = user.Email; userUpdated.IsPaymentCash = user.IsPaymentCash; userUpdated.Phonenumber = user.Phonenumber; userUpdated.Comments = user.Comments; context.SubmitChanges(); } }
public Guid AddNewCustomProperty(CustomProperty customProperty) { using (var context = new InstantStoreDataContext()) { if (!context.PropertyTemplates.Any(x => x.Id == customProperty.TemplateId)) { throw new ModelValidationException("Model.CustomProperty.TemplateNotFound"); } customProperty.Id = Guid.NewGuid(); context.CustomProperties.InsertOnSubmit(customProperty); context.SubmitChanges(); return(customProperty.Id); } }
public void UpdateCustomProperty(Guid id, string data) { // TODO: Check in the model if this property can be upated / deleted. using (var context = new InstantStoreDataContext()) { var property = context.CustomProperties.FirstOrDefault(x => x.Id == id); if (property == null) { throw new ModelValidationException("Model.CustomProperty.TemplateNotFound"); } property.Name = data; context.SubmitChanges(); } }
public void RemoveProductFromCategory(Guid categoryId, IList <Guid> productIdsToRemove) { using (var context = new InstantStoreDataContext()) { if (productIdsToRemove != null && productIdsToRemove.Any()) { foreach (var productId in productIdsToRemove) { var productMappings = context.ProductToCategories.Where(x => x.CategoryId == categoryId && productId == x.ProductId && x.GroupId == null); context.ProductToCategories.DeleteAllOnSubmit(productMappings); } context.SubmitChanges(); CategoryTreeBuilder.RebuidCategoryTreeGroups(context, categoryId); } } }
public Guid AddImage(Image image) { using (var context = new InstantStoreDataContext()) { image.Id = Guid.NewGuid(); context.Images.InsertOnSubmit(image); context.ImageThumbnails.InsertOnSubmit(new ImageThumbnail { Id = image.Id, SmallThumbnail = CreateThumbnail(image.Image1, 64, 64, true), LargeThumbnail = CreateThumbnail(image.Image1, 235, 200, false) }); context.SubmitChanges(); return(image.Id); } }