public IHttpActionResult UpdateCart(string cartid, CartModel[] items) { try { using (var entity = new RoseFashionDBEntities()) { entity.Cart_Product.RemoveRange(entity.Cart_Product.Where(cp => cp.CartID == cartid).ToList()); entity.SaveChanges(); if (items.Length == 0) { return(Ok("OK")); } for (int i = 0; i < items.Length; i++) { var oneitem = items[i]; entity.Cart_Product.Add(new Cart_Product { CartID = cartid, ProductID = oneitem.ProductID, Size = oneitem.Size, Amount = oneitem.Amount, SalePrice = oneitem.SalePrice, OriginalPrice = oneitem.OriginalPrice }); } entity.SaveChanges(); } return(Ok("OK")); } catch (Exception ex) { return(InternalServerError(ex)); } }
public IHttpActionResult DefaultRating(string cartid) { try { using (var entity = new RoseFashionDBEntities()) { var items = entity.Cart_Product.Where(cp => cp.CartID == cartid).ToList(); for (int i = 0; i < items.Count; i++) { string pid = items[i].ProductID; string userid = items[i].Cart.UserID; var existed = entity.Ratings.Where(r => r.UserID == userid && r.ProductID == pid).FirstOrDefault(); if (existed == null) { entity.Ratings.Add(new Rating() { UserID = userid, ProductID = pid, Star = 3 }); } } entity.SaveChanges(); return(Ok()); } } catch (Exception ex) { return(InternalServerError(ex)); } }
public IHttpActionResult DeleteMultipleProduct(string[] pids) { try { using (var entity = new RoseFashionDBEntities()) { for (int i = 0; i < pids.Count(); i++) { string id = pids[i]; var existedproduct = entity.Products.Where(p => p.ProductID == id).FirstOrDefault(); if (existedproduct == null) { return(BadRequest("Product not found.")); } existedproduct.IsDeleted = true; entity.SaveChanges(); } return(Ok("Remove products successfully.")); } } catch (Exception ex) { return(InternalServerError(ex)); } }
public IHttpActionResult GetLastUsedCartID(string userid) { try { using (var entity = new RoseFashionDBEntities()) { var oldcartid = entity.Carts.Where(c => c.UserID == userid && c.IsUsing == true).Select(c => c.CartID).FirstOrDefault(); if (oldcartid == null) { string newcartid = "CR-" + (entity.Carts.Count() + 1); entity.Carts.Add(new Cart { CartID = newcartid, UserID = userid, IsUsing = true }); entity.SaveChanges(); return(Ok(newcartid)); } return(Ok(oldcartid)); } } catch (Exception ex) { return(InternalServerError(ex)); } }
public IHttpActionResult RegisterSocial(UserModel user) { try { using (var entity = new RoseFashionDBEntities()) { int existedemail = entity.Users.Count(u => u.UserID.Equals(user.UserID)); if (existedemail < 1) { entity.Users.Add(new User() { UserID = user.UserID, FullName = user.FullName, Password = user.UserID, Email = user.Email, Role = "user" }); } entity.SaveChanges(); return(Ok("Register successfully.")); } } catch (Exception ex) { return(InternalServerError(ex)); } }
bool UpdateQuantity(string action, CartModel[] items) { using (var entity = new RoseFashionDBEntities()) { for (int i = 0; i < items.Count(); i++) { string pid = items[i].ProductID; string size = items[i].Size; var product = entity.Product_Size_Quantity.Where(p => p.ProductID == pid && p.Size == size).FirstOrDefault(); if (action.Equals("add")) { product.Quantity += items[i].Amount; } else { if (product.Quantity >= items[i].Amount) { product.Quantity -= items[i].Amount; } else { return(false); } } } entity.SaveChanges(); } return(true); }
public IHttpActionResult Register(UserModel user) { try { using (var entity = new RoseFashionDBEntities()) { int existedemail = entity.Users.Count(u => u.Email.Equals(user.Email)); if (existedemail > 0) { return(BadRequest("Email already in use.")); } entity.Users.Add(new User() { UserID = Guid.NewGuid().ToString(), FullName = user.FullName, Email = user.Email, Password = Md5Encryption(user.Password), Role = "user" }); entity.SaveChanges(); return(Ok("Register successfully.")); } } catch (Exception ex) { return(InternalServerError(ex)); } }
public IHttpActionResult CreateGuestUser() { try { using (var entity = new RoseFashionDBEntities()) { //string guestname = "GUEST-" + (entity.Users.Count(u => u.Role == "guest") + 1); string newid = Guid.NewGuid().ToString(); entity.Users.Add(new User() { UserID = newid, FullName = "GUEST", Password = Md5Encryption(newid), Role = "guest" }); entity.SaveChanges(); return(Ok(newid)); //return Ok(guestname + "@gmail.com"); } } catch (Exception ex) { return(InternalServerError(ex)); } }
public IHttpActionResult AddBill(BillModel billinfo) { try { using (var entity = new RoseFashionDBEntities()) { entity.Bills.Add(new Bill { BillID = "BL-" + (entity.Bills.Count() + 1), CartID = billinfo.CartID, PaymentMethod = billinfo.PaymentMethod, OrderDate = DateTime.Now.Date, DeliveryDate = DateTime.Now.Date, ReceiverName = billinfo.ReceiverName, ReceiverPhone = billinfo.ReceiverPhone, DeliveryAddress = billinfo.DeliveryAddress, ProvinceName = billinfo.ProvinceName, DistrictName = billinfo.DistrictName, TotalPrice = billinfo.TotalPrice, DeliveryFee = billinfo.DeliveryFee, Status = billinfo.Status, IsDeleted = false }); var usercart = entity.Carts.Where(c => c.CartID == billinfo.CartID).FirstOrDefault(); usercart.IsUsing = false; entity.SaveChanges(); return(Ok("OK")); } } catch (Exception ex) { return(InternalServerError(ex)); } }
public IHttpActionResult AddProduct(ProductModel newproduct) { try { using (var entity = new RoseFashionDBEntities()) { //kiểm tra sản phẩm đã tồn tại chưa bool existedproduct = entity.Products.Any(p => p.Name == newproduct.Name); if (existedproduct == true) { return(BadRequest("This product already exists.")); } newproduct.ProductID = "PR-" + (entity.Products.Count() + 1); var randomcode = Guid.NewGuid(); string imagename = newproduct.ProductID + "-" + randomcode + ".png"; string imagepath = serverAddress + "/images/" + imagename; entity.Products.Add(new Product { ProductID = newproduct.ProductID, Name = newproduct.Name, CategoryID = newproduct.CategoryID, Image = imagepath, Price = newproduct.Price, DiscountPercent = newproduct.DiscountPercent, ImportDate = DateTime.Now.Date }); entity.SaveChanges(); for (int i = 0; i < newproduct.Size.Count(); i++) { entity.Product_Size_Quantity.Add(new Product_Size_Quantity { ProductID = newproduct.ProductID, Size = newproduct.Size[i], Quantity = newproduct.Quantity[i] }); entity.SaveChanges(); } return(Ok(imagename)); } } catch (Exception ex) { return(InternalServerError(ex)); } }
public IHttpActionResult AddUserRating(RatingModel newrating) { try { using (var entity = new RoseFashionDBEntities()) { var oldrating = entity.Ratings.Where(r => r.UserID == newrating.UserID && r.ProductID == newrating.ProductID).FirstOrDefault(); if (oldrating != null) { oldrating.Title = newrating.Title; oldrating.Comment = newrating.Comment; oldrating.Star = newrating.Star; oldrating.RatingDate = DateTime.Now.Date; entity.SaveChanges(); } else { entity.Ratings.Add(new Rating() { UserID = newrating.UserID, ProductID = newrating.ProductID, Title = newrating.Title, Comment = newrating.Comment, Star = newrating.Star, RatingDate = DateTime.Now.Date }); entity.SaveChanges(); } return(Ok()); } } catch (Exception ex) { return(InternalServerError(ex)); } }
//tính độ tương thích giữa các user private void UserSimilarityCalculation() { using (var entity = new RoseFashionDBEntities()) { var userlist = entity.Users.Where(u => u.Role != "admin").ToList(); for (int i = 0; i < userlist.Count - 1; i++) //không cần xét user cuối { string userid1 = userlist[i].UserID; for (int j = i + 1; j < userlist.Count; j++) //duyệt qua từng người phía sau user đang xét { string userid2 = userlist[j].UserID; //lấy 2 vector của 2 user cần so sánh // vector chỉ lấy rate value của những sp mà cả 2 cùng đánh giá var vectors = entity.fn_GetTwoVetor(userid1, userid2).ToList(); double[] vector1 = new double[vectors.Count]; double[] vector2 = new double[vectors.Count]; for (int k = 0; k < vectors.Count; k++) { vector1[k] = (double)vectors[k].User1Rating; vector2[k] = (double)vectors[k].User2Rating; } //tính và lưu lại độ tương thích của 2 user vào database Similarity newrecord = new Similarity(); newrecord.UserID1 = userid1; newrecord.UserID2 = userid2; newrecord.SimilarityRate = Cosine_Similarity(vector1, vector2); var existedrecord = entity.Similarities.FirstOrDefault(r => r.UserID1 == newrecord.UserID1 && r.UserID2 == newrecord.UserID2); if (existedrecord != null) { existedrecord.SimilarityRate = newrecord.SimilarityRate; } else { entity.Similarities.Add(newrecord); } entity.SaveChanges(); } } } }
public IHttpActionResult EditProduct(ProductModel editedproduct) { try { using (var entity = new RoseFashionDBEntities()) { //kiểm tra sản phẩm có tồn tại hay không var existedproduct = entity.Products.Where(p => p.ProductID == editedproduct.ProductID).FirstOrDefault(); if (existedproduct == null) { return(BadRequest("Product not found.")); } //nếu có => thay đổi các thuộc tính //bỏ ảnh cũ thay ảnh mới string imagename = ""; if (editedproduct.Image.Equals("changed")) { if (RemoveOldImage(existedproduct.Image) == false) { return(BadRequest("Fail to remove image")); } var randomcode = Guid.NewGuid(); imagename = editedproduct.ProductID + "-" + randomcode + ".png"; existedproduct.Image = serverAddress + "/images/" + imagename; } existedproduct.Name = editedproduct.Name; existedproduct.CategoryID = editedproduct.CategoryID; existedproduct.Price = editedproduct.Price; existedproduct.DiscountPercent = editedproduct.DiscountPercent; for (int i = 0; i < editedproduct.Size.Count(); i++) { string size = editedproduct.Size[i]; var currentquantity = entity.Product_Size_Quantity.Where(p => p.ProductID == editedproduct.ProductID && p.Size == size).FirstOrDefault(); currentquantity.Quantity = editedproduct.Quantity[i]; } entity.SaveChanges(); return(Ok(imagename)); } } catch (Exception ex) { return(InternalServerError(ex)); } }
public IHttpActionResult EditCategory(CategoryModel updatecategory) { if (!ModelState.IsValid) { return(BadRequest("Invalid data.")); } using (var entity = new RoseFashionDBEntities()) { var IDCategory = entity.Categories.Where(c => c.CategoryID.Equals(updatecategory.CategoryID)).FirstOrDefault(); if (IDCategory != null) { IDCategory.Name = updatecategory.Name; IDCategory.MainCategory = updatecategory.MainCategory; entity.SaveChanges(); return(Ok("Edit category successfully!")); } return(NotFound()); } }
public IHttpActionResult UpdateBill(BillModel billinfo) { try { using (var entity = new RoseFashionDBEntities()) { var bill = entity.Bills.Where(s => s.BillID == billinfo.BillID).FirstOrDefault(); string oldstatus = bill.Status; bill.Status = billinfo.Status; bill.DeliveryDate = billinfo.DeliveryDate; entity.SaveChanges(); return(Ok(oldstatus)); } } catch (Exception ex) { return(InternalServerError(ex)); } }
public IHttpActionResult ChangePass(UserModel account, string email) { if (!ModelState.IsValid) { return(BadRequest("Invalid data.")); } string pwrHash = GetMd5Hash(account.Password); using (var entity = new RoseFashionDBEntities()) { var Email = entity.Users.Where(c => c.Email.Equals(email)).FirstOrDefault(); if (Email != null) { Email.Password = account.Password; entity.SaveChanges(); return(Ok("Edit password successfully!")); } return(NotFound()); } }
public IHttpActionResult RemoveBill(string billid) { try { using (var entity = new RoseFashionDBEntities()) { var oldbill = entity.Bills.Where(s => s.BillID == billid).FirstOrDefault(); if (oldbill != null) { oldbill.IsDeleted = true; } entity.SaveChanges(); return(Ok()); } } catch (Exception ex) { return(InternalServerError(ex)); } }
public IHttpActionResult DeleteProduct(string productid) { try { using (var entity = new RoseFashionDBEntities()) { var existedproduct = entity.Products.Where(p => p.ProductID == productid).FirstOrDefault(); if (existedproduct == null) { return(BadRequest("Product not found.")); } existedproduct.IsDeleted = true; entity.SaveChanges(); return(Ok("Remove product successfully.")); } } catch (Exception ex) { return(InternalServerError(ex)); } }
public IHttpActionResult AddCategory(CategoryModel newcategory) { try { using (var entity = new RoseFashionDBEntities()) { entity.Categories.Add(new Category() { CategoryID = "CT-" + (entity.Categories.Count() + 1), Name = newcategory.Name, MainCategory = newcategory.MainCategory }); entity.SaveChanges(); return(Ok("Add new category successfully.")); } } catch (Exception ex) { return(InternalServerError(ex)); } }
public IHttpActionResult EditAccount(UserModel account) { if (!ModelState.IsValid) { return(BadRequest("Invalid data.")); } using (var entity = new RoseFashionDBEntities()) { var Email = entity.Users.Where(c => c.Email.Equals(account.Email)).FirstOrDefault(); if (Email != null) { Email.FullName = account.FullName; Email.Address = account.Address; Email.Phone = account.Phone; Email.Province = account.Province; Email.District = account.District; entity.SaveChanges(); return(Ok("Edit account successfully!")); } return(NotFound()); } }
//dự đoán số sao cho các sp mà user chưa mua private void PredictUserRating() { using (var entity = new RoseFashionDBEntities()) { var userlist = entity.Users.Where(u => u.Role != "admin").ToList(); foreach (User user in userlist) { //xóa đi những sp mà user đã mua/đánh giá ra khỏi bảng gợi ý entity.proc_RemoveOldRecommendation(user.UserID); //tìm những sp mà 1 user chưa xem var unratedproduct = entity.fn_GetUnRatedProduct(user.UserID).ToList(); //với mỗi sp foreach (string productid in unratedproduct) { //tìm ra tất cả những người đã đánh giá sp đó // sau đó lọc lấy 10 người trong ds vừa tìm được có độ tương thích lớn nhất so với user đang cần gợi ý var toprating = entity.fn_GetProductRatingFromTopSimilarUser(user.UserID, productid).ToList(); //nếu ko có user nào đánh giá cho sp này => ds rỗng if (toprating.Count != 0) { //dự đoán rating của user cho sp đó double numerator = 0; //tử số double denominator = 0; //mẫu số for (int i = 0; i < toprating.Count; i++) { numerator += (double)toprating[i].Star * (double)toprating[i].SimilarityRate; denominator += (double)toprating[i].SimilarityRate; } if (denominator == 0) { continue; } double predictrating = numerator / denominator; //tổng số sao / tổng số người đánh giá //lưu kết quả dự đoán vào database var oldrecord = entity.Recommendations.FirstOrDefault(r => r.UserID == user.UserID && r.ProductID == productid); if (oldrecord != null) { oldrecord.PredictedStar = predictrating; } else { entity.Recommendations.Add(new Recommendation { UserID = user.UserID, ProductID = productid, PredictedStar = predictrating }); } entity.SaveChanges(); } } } } }