public async Task <IActionResult> AddOrRemoveFavorite(FavoritsDto data) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var _userId = User.FindFirstValue(ClaimTypes.NameIdentifier); // will give the user's userId // var _userId = User.Identity.GetUserId(); Guid clientId = (await Db.TblClient.FirstOrDefaultAsync(q => q.IdentityId == _userId)).PkClientId; var existed = (await Db.TblFavorites.FirstOrDefaultAsync(e => e.FkClientId == clientId && e.FkItemsId == data.ItemID)); if (existed != null) { var saved = await RemoveItemFromFavorites(data.ItemID, clientId); //return saved ? Ok() : BadRequest(); if (saved) { return(Ok(new { message = " Item has been Deleted From Your Favorite Successfully ..! " })); } else { return(BadRequest(" Something Went Wrong :( ")); } } else { var newFavorite = new TblFavorites() { PkFavoritesId = Guid.NewGuid(), FkItemsId = data.ItemID, FkClientId = clientId, }; await Db.TblFavorites.AddAsync(newFavorite); await Db.SaveChangesAsync(); var location = new Uri(HttpContext.Request.Path + "/" + newFavorite.PkFavoritesId.ToString());//I Edit This line data.pk_Favorites_Id = newFavorite.PkFavoritesId; data.ClientId = newFavorite.FkClientId; return(Created(location, data)); } } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public async Task <IActionResult> ClientEditProfile(ClientEditProfileDto data) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } HelpFunctionsController helpFunction = new HelpFunctionsController(); var IdentityClientId = User.FindFirstValue(ClaimTypes.NameIdentifier);// will give the user's userId // var IdentityClientId = User.Identity.GetUserId(); var IdentityEmail = string.Empty; if (HttpContext.User.Identity is ClaimsIdentity identity) { IdentityEmail = identity.FindFirst(ClaimTypes.Name).Value; } // var IdentityEmail = await Db.AspNetUsers.FirstOrDefaultAsync(q => q.Id == IdentityClientId); var clientData = await Db.TblClient.FirstOrDefaultAsync(c => c.IdentityId == IdentityClientId); clientData.Name = data.Name; clientData.ManagerName = data.ManagerName; clientData.Mobile1 = data.Mobile1; clientData.Mobile2 = data.Mobile2; clientData.Phone = data.Phone; clientData.Email = data.Email; IdentityEmail = data.Email; clientData.Fax = data.Fax; clientData.ModifiedAt = DateTime.Now; clientData.ModifiedBy = IdentityClientId; await Db.SaveChangesAsync(); return(Ok(data)); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public async Task <IActionResult> AddOrder(OrdersDto data) { if (!ModelState.IsValid || data.TotalCost > data.TotalAmount) { return(BadRequest(ModelState)); } try { var ClientIdentityId = User.FindFirstValue(ClaimTypes.NameIdentifier);// will give the user's userId // var ClientIdentityId = User.Identity.GetUserId(); var clientId = (await Db.TblClient.FirstOrDefaultAsync(q => q.IdentityId == ClientIdentityId)).PkClientId; var orderData = new TblOrders { PkOrdersId = data.OrderId = Guid.NewGuid(), OrderDate = data.Order_Date = DateTime.Now, TotalAmount = data.TotalAmount, TotalCost = data.TotalCost, FkClientsOrdersClientId = data.ClientId = clientId, CraetedAt = data.CraetedAt = DateTime.Now, CreatedBy = data.CreatedBy = data.CreatedBy = ClientIdentityId, ModifiedAt = data.ModifiedAt = DateTime.Now, ModifiedBy = data.ModifiedBy = data.ModifiedBy = ClientIdentityId, OrderIsOrder = data.Order_IsOrder = true, IsCancelled = data.IsCancelled = false, IsDeleted = data.IsDeleted = false, OnDelivery = data.OnDelivery = false, OrderIsPaid = data.Order_IsPaid = false, OrderNo = data.OrderNo = Db.TblOrders.Count() + 1, }; await Db.TblOrders.AddAsync(orderData); if (orderData.TblOrderDetails != null) { foreach (var item in data.OrderDetails) { var itemData = (await Db.TblItems.Where(i => i.PkItemsId == item.fk_Items_Id).FirstOrDefaultAsync()); if (item.Quantity > itemData.Available) { return(BadRequest("please select a valid quantity for item :" + itemData.Name)); } } foreach (var item in data.OrderDetails) { var itemData = (await Db.TblItems.Where(i => i.PkItemsId == item.fk_Items_Id).FirstOrDefaultAsync()); itemData.BookedUp = itemData.BookedUp + item.Quantity; await Db.SaveChangesAsync(); var orderDetailsData = new TblOrderDetails { PkOrderDetailsId = item.OrderDetailId = Guid.NewGuid(), OrderId = orderData.PkOrdersId, FkItemsId = item.fk_Items_Id, Quantity = item.Quantity, CraetedAt = item.CraetedAt = DateTime.Now, CreatedBy = item.ModifiedBy = ClientIdentityId, ModifiedAt = item.ModifiedAt = DateTime.Now, ModifiedBy = item.ModifiedBy = ClientIdentityId }; await Db.TblOrderDetails.AddAsync(orderDetailsData); } } await Db.SaveChangesAsync(); return(Ok(new { message = " Your order has been placed successfully :) With OrderNo : (" + data.OrderNo + ")" })); } catch (Exception ex) { return(BadRequest(ex.Message)); } }