示例#1
0
        public SupplyResponse Put(int id, [FromBody] UserRequest user)
        {
            var dbUser = _dbContext.User.SingleOrDefault(p => p.UserId == id);

            if (dbUser == null)
            {
                return(SupplyResponse.NotFound("user", id + ""));
            }
            if (string.IsNullOrWhiteSpace(user.UserName))
            {
                return(SupplyResponse.BadRequest("Required Field is Empty"));
            }

            dbUser.Name     = user.Name;
            dbUser.UserName = user.UserName;
            dbUser.UserType = user.UserType;

            var oldPw = dbUser.Password;

            user.Password = string.IsNullOrWhiteSpace(user.Password)
                ? oldPw
                : HashUtilities.HashPassword(user.Password);

            _dbContext.SaveChanges();
            return(Get(id));
        }
示例#2
0
        public SupplyResponse Post([FromBody] Announcement data)
        {
            if (string.IsNullOrWhiteSpace(data.Message) || string.IsNullOrWhiteSpace(data.Title) ||
                data.Target == null || !data.Target.Any())
            {
                return(SupplyResponse.RequiredFieldEmpty());
            }

            if (data.RemovalTime < DateTime.Now)
            {
                return(SupplyResponse.BadRequest("Removal Time should be a future date"));
            }

            _dbContext.DataCache.Add(new DataCache
            {
                CacheType = "Announcement",
                CacheTime = DateTime.Now,
                Content   = JsonConvert.SerializeObject(data, new JsonSerializerSettings
                {
                    ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                    NullValueHandling     = NullValueHandling.Ignore,
                    ContractResolver      = new CamelCasePropertyNamesContractResolver()
                }),
                RemovalTime = data.RemovalTime
            });
            _dbContext.SaveChanges();
            _hubContext.Clients.All.SendAsync("ReceiveAnnouncement", data.Target, "A new announcement broadcasted");
            return(Get());
        }
示例#3
0
        public SupplyResponse Put(int id, [FromBody] ICollection <ItemRequest> itemRequest)
        {
            var request = _dbContext.Request.Include(p => p.RequestItem).SingleOrDefault(p => p.RequestId == id);

            if (request == null)
            {
                return(SupplyResponse.NotFound("Request", id + ""));
            }

            if (itemRequest == null || !itemRequest.Any())
            {
                return(SupplyResponse.BadRequest("Request Item cannot be empty."));
            }

            ICollection <RequestItem> requestItems;

            if ((requestItems = request.RequestItem) != null)
            {
                foreach (var items in requestItems)
                {
                    _dbContext.Remove(items);
                }
            }

            _dbContext.SaveChanges();

            var itemMap = new Dictionary <int, int>();

            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
                {
                    RequestId     = request.RequestId,
                    VirtualItemId = itemId,
                    Quantity      = qty
                };
                _dbContext.RequestItem.Add(requestItem);
                _dbContext.SaveChanges();
            }

            return(Get(request.RequestId));
        }
示例#4
0
 public SupplyResponse Post([FromBody] Supplier supplier)
 {
     if (string.IsNullOrWhiteSpace(supplier.SupplierName) || string.IsNullOrWhiteSpace(supplier.SupplierContactPerson) || string.IsNullOrWhiteSpace(supplier.SupplierAddress))
     {
         return(SupplyResponse.BadRequest("Required Field is Empty"));
     }
     _dbContext.Supplier.Add(supplier);
     _dbContext.SaveChanges();
     return(Get(supplier.SupplierId));
 }
示例#5
0
        public SupplyResponse Put(int id, [FromBody] Supplier supplier)
        {
            var entity = _dbContext.Supplier.AsNoTracking().SingleOrDefault(p => p.SupplierId == id);

            if (entity == null)
            {
                return(SupplyResponse.NotFound("supplier", id + ""));
            }
            if (string.IsNullOrWhiteSpace(supplier.SupplierName))
            {
                return(SupplyResponse.BadRequest("Required Field is Empty"));
            }
            supplier.SupplierId = id;
            _dbContext.Attach(supplier);
            _dbContext.Entry(supplier).State = EntityState.Modified;
            _dbContext.SaveChanges();
            return(Get(id));
        }
示例#6
0
        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));
        }
示例#7
0
        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 + ""));
        }