public SupplyResponse CreateToken([FromBody] LoginRequest login) { var response = SupplyResponse.Fail("Unauthorize", "Your are not the user in the system."); var user = Authenticate(login); if (user != null) { var expiry = DateTime.Now.AddDays(1); var tokenString = BuildToken(expiry, user); var restaurantManager = user.RestaurantManager; dynamic workplace = null; if (restaurantManager != null) { workplace = new { restaurantId = restaurantManager.RestaurantId, stockId = restaurantManager.Restaurant.StockId }; } response = SupplyResponse.Ok(new { token = tokenString, userType = user.UserType, expiry, user, workplace }); } return(response); }
public SupplyResponse Get() { var currentUser = HttpContext.User; var dbUser = _dbContext.User.Include(p => p.RestaurantManager).ThenInclude(p => p.Restaurant) .SingleOrDefault(p => currentUser.FindFirst(ClaimTypes.Name).Value.Equals(p.UserName)); if (dbUser == null) { return(SupplyResponse.Fail("Unauthorize", "Your are not the user in the system.")); } var restaurantManager = dbUser.RestaurantManager; if (restaurantManager == null) { return(SupplyResponse.Fail("Unauthorize", "Your are not the restaurant manager.")); } var restaurantId = restaurantManager.Restaurant.RestaurantId; var requests = _dbContext.Request.Include(p => p.User).Include(p => p.RequestItem) .ThenInclude(p => p.VirtualItem).Include(p => p.RequestMap) .Where(p => p.RestaurantId == restaurantId) .Select(p => p); foreach (var request in requests) { foreach (var requestItem in request.RequestItem) { requestItem.VirtualItemName = requestItem.VirtualItem.VirtualItemName; requestItem.RequestVirtualItemId = requestItem.VirtualItem.VirtualItemId; } } return(SupplyResponse.Ok(requests)); }
public SupplyResponse Get() { var currentUser = HttpContext.User; var dbUser = _dbContext.User.Include(p => p.RestaurantManager).ThenInclude(p => p.Restaurant) .SingleOrDefault(p => currentUser.FindFirst(ClaimTypes.Name).Value.Equals(p.UserName)); if (dbUser == null) { return(SupplyResponse.Fail("Unauthorize", "Your are not the user in the system.")); } if (dbUser.UserType == UserType.RestaurantManager) { var restaurantManager = dbUser.RestaurantManager; if (restaurantManager == null) { return(SupplyResponse.Fail("Unauthorize", "Your are not the restaurant manager of any restaurant.")); } return(SupplyResponse.Ok(new { stock = restaurantManager.Restaurant.StockId })); } var id = _dbContext.Stock.FirstOrDefault(p => p.StockType == StockType.WarehouseStock).StockId; return(SupplyResponse.Ok(new { stock = id })); }
public SupplyResponse Delete(string id) { try { var entity = _dbContext.Item.SingleOrDefault(p => p.SupplierItemId.Equals(id)); if (entity == null) { return(SupplyResponse.NotFound("item", id + "")); } _dbContext.Remove(entity); _dbContext.SaveChanges(); return(SupplyResponse.Ok()); } catch { return(SupplyResponse.Fail("Item in use", "This item referenced by request or order, you cannot remove it.")); } }
public SupplyResponse Delete(int id) { try { var agreement = _dbContext.Agreement.SingleOrDefault(p => p.AgreementId == id); if (agreement == null) { return(SupplyResponse.NotFound("Agreement", id + "")); } _dbContext.Remove(agreement); _dbContext.SaveChanges(); return(SupplyResponse.Ok()); } catch { return(SupplyResponse.Fail("Agreement in use", "This agreement had referenced by orders, you cannot remove it.")); } }
public SupplyResponse Refresh() { var currentUser = HttpContext.User; var dbUser = _dbContext.User.Include(p => p.RestaurantManager).ThenInclude(p => p.Restaurant) .SingleOrDefault(p => currentUser.FindFirst(ClaimTypes.Name).Value.Equals(p.UserName)); if (dbUser == null) { return(SupplyResponse.Fail("Unauthorize", "Your are not the user in the system.")); } var expiry = DateTime.Now.AddDays(1); var tokenString = BuildToken(expiry, dbUser); var restaurantManager = dbUser.RestaurantManager; dynamic workplace = null; if (restaurantManager != null) { workplace = new { restaurantId = restaurantManager.RestaurantId, stockId = restaurantManager.Restaurant.StockId }; } var resp = new { token = tokenString, userType = dbUser.UserType, expiry, dbUser, workplace }; var response = SupplyResponse.Ok(resp); return(response); }
public SupplyResponse AddRestaurantManager(int id, [FromBody] IntIdRequest idRequest) { if (id == 0 || idRequest.Id == 0) { return(SupplyResponse.RequiredFieldEmpty()); } var restaurant = _dbContext.Restaurant.SingleOrDefault(p => id == p.RestaurantId); var user = _dbContext.User.SingleOrDefault(p => p.UserId == idRequest.Id); if (restaurant == null) { return(SupplyResponse.NotFound("restaurant", id + "")); } if (user == null) { return(SupplyResponse.NotFound("user", idRequest.Id + "")); } var restaurantManager = _dbContext.RestaurantManager.Include(p => p.Restaurant) .SingleOrDefault(p => p.UserId == idRequest.Id); if (restaurantManager != null) { return(SupplyResponse.Fail("Already a Manager", "This user is already a manager of restaurant: " + restaurantManager.Restaurant.RestaurantName)); } restaurantManager = new RestaurantManager { RestaurantId = id, UserId = idRequest.Id }; _dbContext.RestaurantManager.Add(restaurantManager); _dbContext.SaveChanges(); return(Get(id)); }
public SupplyResponse Post([FromBody] ICollection <ItemRequest> itemRequest) { var currentUser = HttpContext.User; var dbUser = _dbContext.User.Include(p => p.RestaurantManager).ThenInclude(p => p.Restaurant) .SingleOrDefault(p => currentUser.FindFirst(ClaimTypes.Name).Value.Equals(p.UserName)); if (dbUser == null) { return(SupplyResponse.Fail("Unauthorize", "Your are not the user in the system.")); } var restaurantManager = dbUser.RestaurantManager; if (restaurantManager == null) { return(SupplyResponse.Fail("Unauthorize", "Your are not the restaurant manager.")); } if (itemRequest == null || !itemRequest.Any()) { return(SupplyResponse.BadRequest("Request Item cannot be empty.")); } var itemMap = new Dictionary <int, int>(); var itemList = new List <RequestItem>(); foreach (var item in itemRequest) { var virtualItem = _dbContext.VirtualItem.SingleOrDefault(p => p.VirtualItemId.Equals(item.VirtualItemId)); if (virtualItem == null) { return(SupplyResponse.NotFound("virtual item", item.VirtualItemId)); } if (itemMap.ContainsKey(virtualItem.Id)) { itemMap[virtualItem.Id] += item.Quantity; } else { itemMap.Add(virtualItem.Id, item.Quantity); } } foreach (var(itemId, qty) in itemMap) { var requestItem = new RequestItem { VirtualItemId = itemId, Quantity = qty }; itemList.Add(requestItem); } var restaurantId = restaurantManager.RestaurantId; var userId = dbUser.UserId; var request = new Models.Request { RestaurantId = restaurantId, RequestCreator = userId, CreateTime = DateTime.Now }; _dbContext.Request.Add(request); _dbContext.SaveChanges(); var requestId = request.RequestId; _dbContext.Entry(request).State = EntityState.Detached; foreach (var item in itemList) { item.RequestId = requestId; _dbContext.RequestItem.Add(item); _dbContext.SaveChanges(); _dbContext.Entry(item).State = EntityState.Detached; } return(Get(request.RequestId)); }
public SupplyResponse Post([FromBody] AgreementWrapper agreement) { var currentUser = HttpContext.User; var dbUser = _dbContext.User.Include(p => p.RestaurantManager).ThenInclude(p => p.Restaurant) .SingleOrDefault(p => currentUser.FindFirst(ClaimTypes.Name).Value.Equals(p.UserName)); if (dbUser == null) { return(SupplyResponse.Fail("Unauthorize", "Your are not the user in the system.")); } if (agreement.Details == null || agreement.Items == null || agreement.SupplierId == 0) { return(SupplyResponse.RequiredFieldEmpty()); } if (agreement.StartDate > agreement.ExpiryDate) { return(SupplyResponse.BadRequest("Start date cannot be later than Expiry Date")); } //BPA if (agreement.AgreementType == AgreementType.Blanket) { //Get data from request ICollection <QuantityItems> items = new List <QuantityItems>(); BlanketPurchaseAgreementDetails details; try { foreach (var item in agreement.Items) { items.Add(item.ToObject <QuantityItems>()); } if (!items.Any()) { return(SupplyResponse.BadRequest("Agreement Line is Empty")); } details = agreement.Details.ToObject <BlanketPurchaseAgreementDetails>(); } catch (Exception e) { Console.WriteLine(e.ToString()); return(SupplyResponse.BadRequest("Request Format Fail")); } //Verfiy&Process request object var dbLine = new Dictionary <string, BlanketPurchaseAgreementLine>(); foreach (var item in items) { if (item.MinimumQuantity <= 0 && item.PromisedQuantity <= 0) { return(SupplyResponse.BadRequest($"Item {item.SupplierItemId} has a zero or negative quantity")); } var dbItem = _dbContext.Item.SingleOrDefault(p => item.SupplierItemId == p.SupplierItemId); if (dbItem == null) { return(SupplyResponse.NotFound("supplier item", item.SupplierItemId)); } if (dbLine.ContainsKey(item.SupplierItemId)) { return(SupplyResponse.DuplicateEntry("Request Item", item.SupplierItemId)); } dbLine[item.SupplierItemId] = new BlanketPurchaseAgreementLine { ItemId = dbItem.Id, MinimumQuantity = item.MinimumQuantity, PromisedQuantity = item.PromisedQuantity, Price = item.Price, Unit = item.Unit, }; } //Create Agreement Object var dbAgreement = new Agreement { AgreementType = AgreementType.Blanket, Currency = agreement.Currency, StartDate = agreement.StartDate, ExpiryDate = agreement.ExpiryDate, SupplierId = agreement.SupplierId, CreateBy = dbUser.UserId, TermsAndCondition = agreement.TermsAndCondition }; _dbContext.Agreement.Add(dbAgreement); _dbContext.SaveChanges(); var agreementId = dbAgreement.AgreementId; _dbContext.Entry(dbAgreement).State = EntityState.Detached; details.AgreementId = agreementId; _dbContext.BlanketPurchaseAgreementDetails.Add(details); _dbContext.SaveChanges(); foreach (var line in dbLine.Values) { line.AgreementId = agreementId; var entry = _dbContext.BlanketPurchaseAgreementLine.Add(line); _dbContext.SaveChanges(); entry.State = EntityState.Detached; } return(Get(agreementId)); } //CPA if (agreement.AgreementType == AgreementType.Contract) { ICollection <QuantityItems> items = new List <QuantityItems>(); ContractPurchaseAgreementDetails details; try { foreach (var item in agreement.Items) { items.Add(item.ToObject <QuantityItems>()); } if (!items.Any()) { return(SupplyResponse.BadRequest("Agreement Line is Empty")); } details = agreement.Details.ToObject <ContractPurchaseAgreementDetails>(); } catch (Exception e) { Console.WriteLine(e.ToString()); return(SupplyResponse.BadRequest("Request Format Fail")); } var dbLine = new List <ContractPurchaseAgreementLine>(); foreach (var item in items) { var dbItem = _dbContext.Item.SingleOrDefault(p => item.SupplierItemId == p.SupplierItemId); if (dbItem == null) { return(SupplyResponse.NotFound("supplier item", item.SupplierItemId)); } dbLine.Add(new ContractPurchaseAgreementLine() { ItemId = dbItem.Id, }); } var dbAgreement = new Agreement { AgreementType = AgreementType.Contract, Currency = agreement.Currency, StartDate = agreement.StartDate, ExpiryDate = agreement.ExpiryDate, SupplierId = agreement.SupplierId, CreateBy = dbUser.UserId, TermsAndCondition = agreement.TermsAndCondition }; _dbContext.Agreement.Add(dbAgreement); _dbContext.SaveChanges(); var agreementId = dbAgreement.AgreementId; _dbContext.Entry(dbAgreement).State = EntityState.Detached; details.AgreementId = agreementId; _dbContext.ContractPurchaseAgreementDetails.Add(details); _dbContext.SaveChanges(); foreach (var line in dbLine) { line.AgreementId = agreementId; var entry = _dbContext.ContractPurchaseAgreementLine.Add(line); _dbContext.SaveChanges(); entry.State = EntityState.Detached; } return(Get(agreementId)); } //PPA if (agreement.AgreementType == AgreementType.Planned) { ICollection <QuantityItems> items = new List <QuantityItems>(); PlannedPurchaseAgreementDetails details; try { foreach (var item in agreement.Items) { items.Add(item.ToObject <QuantityItems>()); } if (!items.Any()) { return(SupplyResponse.BadRequest("Agreement Line is Empty")); } details = agreement.Details.ToObject <PlannedPurchaseAgreementDetails>(); } catch (Exception e) { Console.WriteLine(e.ToString()); return(SupplyResponse.BadRequest("Request Format Fail")); } var dbLine = new Dictionary <string, PlannedPurchaseAgreementLine>(); foreach (var item in items) { if (item.Quantity <= 0) { return(SupplyResponse.BadRequest($"Item {item.SupplierItemId} has a zero or negative quantity")); } var dbItem = _dbContext.Item.SingleOrDefault(p => item.SupplierItemId == p.SupplierItemId); if (dbItem == null) { return(SupplyResponse.NotFound("supplier item", item.SupplierItemId)); } if (dbLine.ContainsKey(item.SupplierItemId)) { return(SupplyResponse.DuplicateEntry("Request Item", item.SupplierItemId)); } dbLine[item.SupplierItemId] = new PlannedPurchaseAgreementLine { ItemId = dbItem.Id, Quantity = item.Quantity, Price = item.Price, Unit = item.Unit }; } var dbAgreement = new Agreement { AgreementType = AgreementType.Planned, Currency = agreement.Currency, StartDate = agreement.StartDate, ExpiryDate = agreement.ExpiryDate, SupplierId = agreement.SupplierId, CreateBy = dbUser.UserId, TermsAndCondition = agreement.TermsAndCondition }; _dbContext.Agreement.Add(dbAgreement); _dbContext.SaveChanges(); var agreementId = dbAgreement.AgreementId; _dbContext.Entry(dbAgreement).State = EntityState.Detached; details.AgreementId = agreementId; _dbContext.PlannedPurchaseAgreementDetails.Add(details); _dbContext.SaveChanges(); foreach (var line in dbLine.Values) { line.AgreementId = agreementId; var entry = _dbContext.PlannedPurchaseAgreementLine.Add(line); _dbContext.SaveChanges(); entry.State = EntityState.Detached; } return(Get(agreementId)); } return(SupplyResponse.NotFound("Agreement Type", agreement.AgreementType + "")); }