public async Task <IActionResult> PutStock(int id, StockDTO stockDTO)
        {
            if (id != stockDTO.ID)
            {
                return(BadRequest());
            }

            _context.Entry(stockDTO).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!StockExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Beispiel #2
0
        public async Task <IActionResult> PutProducts(int id, Products products)
        {
            if (id != products.ProductId)
            {
                return(BadRequest());
            }

            _context.Entry(products).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductsExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> PutTransaction(int id, TransactionDTO transactionDto)
        {
            if (id != transactionDto.ID)
            {
                return(BadRequest());
            }

            _context.Entry(transactionDto).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TransactionExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Beispiel #4
0
        public async Task <IHttpActionResult> PutInventory(int id, Inventory inventory)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var request = db.Inventories.Find(id);

            request.Quantity -= inventory.Quantity;

            if (id != inventory.ID)
            {
                return(BadRequest());
            }

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!InventoryExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public async Task <IActionResult> Create([Bind("ProductId,ProductName,Description,Price,Quantity,Category")] Product product)
        {
            if (ModelState.IsValid)
            {
                _context.Add(product);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(product));
        }
        public async Task <IActionResult> Create([Bind("Created,Id,Name,Description")] InventoryItem inventoryItem)
        {
            if (ModelState.IsValid)
            {
                _context.Add(inventoryItem);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(inventoryItem));
        }
Beispiel #7
0
        public async Task <ActionResult> DeleteCar(int id)
        {
            var car = await _context.Cars.FindAsync(id);

            if (car == null)
            {
                return(NotFound());
            }
            else
            {
                _context.Cars.Remove(car);
                await _context.SaveChangesAsync();

                return(NoContent());
                //204 status code -- successful and the API is not returning any content
            }
        }
        public async Task <Staff> AddStaff(Staff staff)
        {
            try
            {
                if (staff != null)
                {
                    await _inventoryDBContext.Staffs.AddRangeAsync(staff);

                    await _inventoryDBContext.SaveChangesAsync();

                    return(staff);
                }
                return(staff);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Method used for create and update brand, Pass Brand_Id=0 for create and Brand_Id>0 for update
        /// </summary>
        /// <param name="BrandObj"></param>
        /// <returns>Response Object</returns>
        public async Task <ResponseViewModel <BrandViewModel> > CreateAndUpdate(BrandViewModel BrandObj)
        {
            ResponseViewModel <BrandViewModel> ResObj = new ResponseViewModel <BrandViewModel>();

            try
            {
                if (BrandObj != null)
                {
                    bool IsNew = false;
                    InventoryDBContext dbContext = new InventoryDBContext(await connMethod.GetConnectionString(BrandObj.ConnectionString));
                    /*Check if already exists*/
                    Brand BrandDBObj  = dbContext.Brand.Where(x => x.Brand_Id == BrandObj.Brand_Id).FirstOrDefault();
                    bool  IsDuplicate = dbContext.Brand.Where(x => x.Name == BrandObj.Name && x.Brand_Id != BrandObj.Brand_Id).FirstOrDefault() != null?true:false;
                    if (!IsDuplicate)
                    {
                        if (BrandDBObj == null)
                        {
                            BrandDBObj = new Brand();
                            IsNew      = true;
                        }
                        BrandDBObj.Name = BrandObj.Name;
                        if (IsNew)
                        {
                            await dbContext.Brand.AddAsync(BrandDBObj);
                        }
                        await dbContext.SaveChangesAsync();

                        BrandObj.Brand_Id = BrandDBObj.Brand_Id;
                        ResObj.IsSuccess  = true;
                        List <BrandViewModel> BrandList = new List <BrandViewModel>();
                        BrandList.Add(BrandObj);
                        ResObj.Data = BrandList;
                    }
                    else
                    {
                        ResObj.IsSuccess    = false;
                        ResObj.ErrorCode    = 500;
                        ResObj.ErrorDetails = "Duplicate brand name.";
                    }
                }
                else
                {
                    ResObj.IsSuccess    = false;
                    ResObj.ErrorCode    = 400;
                    ResObj.ErrorDetails = "Parameter not provided.";
                }
            }
            catch (Exception ex) {
                ResObj.IsSuccess    = false;
                ResObj.ErrorCode    = 500;
                ResObj.ErrorDetails = ex.ToString();
            }
            return(ResObj);
        }
        public async Task <Product> AddProduct(Product product)
        {
            try
            {
                if (product != null)
                {
                    product.IsActive    = true;
                    product.CreatedDate = DateTime.Now;
                    await _inventoryDBContext.Products.AddRangeAsync(product);

                    await _inventoryDBContext.SaveChangesAsync();

                    return(product);
                }
                return(product);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #11
0
        public async Task <ResponseViewModel <QuantityViewModel> > CreateAndUpdate(QuantityViewModel QauntityObj)
        {
            ResponseViewModel <QuantityViewModel> ResObj = new ResponseViewModel <QuantityViewModel>();

            try
            {
                if (QauntityObj != null)
                {
                    bool IsNew = false;
                    InventoryDBContext dbContext = new InventoryDBContext(await connMethod.GetConnectionString(QauntityObj.ConnectionString));
                    /*Check if already exists*/
                    Brand_Quantity_Time_Received QuantityDBObj = dbContext.Brand_Quantity_Time_Received.Where(x => x.Inventory_Id == QauntityObj.Brand_Id).FirstOrDefault();
                    if (QuantityDBObj == null)
                    {
                        QuantityDBObj = new Brand_Quantity_Time_Received();
                        IsNew         = true;
                    }
                    QuantityDBObj.Brand_Id      = QauntityObj.Brand_Id;
                    QuantityDBObj.Quantity      = QauntityObj.Quantity;
                    QuantityDBObj.Time_Received = QauntityObj.Time_Received;
                    if (IsNew)
                    {
                        await dbContext.Brand_Quantity_Time_Received.AddAsync(QuantityDBObj);
                    }
                    await dbContext.SaveChangesAsync();

                    QauntityObj.Inventory_Id = QuantityDBObj.Inventory_Id;
                    ResObj.IsSuccess         = true;
                    List <QuantityViewModel> QuantityList = new List <QuantityViewModel>();
                    QuantityList.Add(QauntityObj);
                    ResObj.Data = QuantityList;
                }
                else
                {
                    ResObj.IsSuccess    = false;
                    ResObj.ErrorCode    = 400;
                    ResObj.ErrorDetails = "Parameter not provided.";
                }
            }
            catch (Exception ex)
            {
                ResObj.IsSuccess    = false;
                ResObj.ErrorCode    = 500;
                ResObj.ErrorDetails = ex.ToString();
            }
            return(ResObj);
        }
Beispiel #12
0
        public async Task <IHttpActionResult> PostTransfer(Transfer transfer)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // check
            if (Utility.IsSameString(transfer.Sender, transfer.Receiver))
            {
                return(BadRequest("User already own the item!"));
            }

            // check that it is transfered to a valid user of inventory
            if (!db.PIDs.Any(p => p.name == transfer.Receiver))
            {
                return(BadRequest("The receiver is not a valid user of inventory"));
            }

            // check that it is transfered from current owner
            string user = transfer.Sender;

            if (!db.Items.Any(item => item.Custodian == transfer.Sender && item.Ptag == transfer.Ptag))
            {
                return(BadRequest("The item either doesn't exist or has a different owner."));
            }

            // check that the request is not pending
            if (db.Transfers.Any(t => t.Ptag == transfer.Ptag))
            {
                return(BadRequest("This request is still pending."));
            }

            // initial request was not approved
            transfer.Status = 0;
            // generate timestamp for that request
            // may need to change server time to make this local
            int unixTimestamp = (int)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;

            transfer.Time = unixTimestamp.ToString();

            db.Transfers.Add(transfer);
            await db.SaveChangesAsync();

            return(CreatedAtRoute("DefaultApi", new { ptag = transfer.Ptag }, transfer));
        }
Beispiel #13
0
        public async Task <ResponseViewModel <QuantityViewModel> > Delete(QuantityViewModel QuantityObj)
        {
            ResponseViewModel <QuantityViewModel> ResObj = new ResponseViewModel <QuantityViewModel>();

            try
            {
                if (QuantityObj != null)
                {
                    InventoryDBContext dbContext = new InventoryDBContext(await connMethod.GetConnectionString(QuantityObj.ConnectionString));
                    /*Check if already exists*/
                    Brand_Quantity_Time_Received QuantityDBObj = dbContext.Brand_Quantity_Time_Received.Where(x => x.Inventory_Id == QuantityObj.Inventory_Id).FirstOrDefault();
                    if (QuantityDBObj != null)
                    {
                        dbContext.Brand_Quantity_Time_Received.Remove(QuantityDBObj);
                        await dbContext.SaveChangesAsync();

                        ResObj.IsSuccess = true;
                    }
                    else
                    {
                        ResObj.IsSuccess    = false;
                        ResObj.ErrorCode    = 404;
                        ResObj.ErrorDetails = "Record not found.";
                    }
                }
                else
                {
                    ResObj.IsSuccess    = false;
                    ResObj.ErrorCode    = 400;
                    ResObj.ErrorDetails = "Parameter not provided.";
                }
            }
            catch (Exception ex)
            {
                ResObj.IsSuccess    = false;
                ResObj.ErrorCode    = 500;
                ResObj.ErrorDetails = ex.ToString();
            }
            return(ResObj);
        }
 public async Task <int> Complete()
 {
     return(await _context.SaveChangesAsync());
 }