Exemplo n.º 1
0
        public IHttpActionResult PutEmployee(int id, Employee employee)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != employee.Id)
            {
                return(BadRequest());
            }

            db.Entry(employee).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!EmployeeExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Exemplo n.º 2
0
 public ActionResult <Product> Post([FromBody] Product product)
 {
     product.CreatedAt = DateTime.Now;
     product.CreatedBy = User.Identity.Name;
     _context.Products.Add(product);
     _context.SaveChanges();
     return(product);
 }
        public IActionResult Post([FromBody] Transaction transaction)
        {
            //todo - refactor whole process

            try
            {
                //get balance
                var balance = _context.Balances.FirstOrDefault(x => x.ProductId == transaction.ProductId);

                if (transaction.Type == TransactionType.SELL && balance.StockQuantity < transaction.Quantity)
                {
                    throw new Exception("This quantity is not available");
                }

                transaction.CreatedAt = DateTime.Now;
                transaction.CreatedBy = User.Identity.Name;
                _context.Transactions.Add(transaction);

                if (balance == null)
                {
                    _context.Add(new Balance
                    {
                        ProductId      = transaction.ProductId,
                        StockQuantity  = transaction.Quantity,
                        TotalQuantity  = transaction.Quantity,
                        PurchaseAmount = transaction.Amount,
                    });
                }
                else
                {
                    if (transaction.Type == TransactionType.BUY)
                    {
                        balance.StockQuantity  += transaction.Quantity;
                        balance.TotalQuantity  += transaction.Quantity;
                        balance.PurchaseAmount += transaction.Amount;
                    }
                    else
                    {
                        balance.StockQuantity -= transaction.Quantity;
                        balance.SellingAmount += transaction.Amount;
                    }
                    _context.Update(balance);
                }

                _context.SaveChanges();
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex.StackTrace));
            }

            return(Ok(transaction));
        }
        public ActionResult <Company> Post([FromBody] Company company)
        {
            company.CreatedAt = DateTime.Now;
            _context.Companies.Add(company);
            _context.SaveChanges();
            var accessright = new UserAccessRight
            {
                CompanyId      = company.Id,
                DefaultCompany = true,
                IsActive       = true,
                Role           = AccessRight.Admin,
                UserId         = CurrentUserId,
                CreatedAt      = DateTime.Now
            };

            _context.Add(accessright);
            _context.SaveChanges();
            return(company);
        }
        public async Task <Order> AddOrder(Order orderdetails)
        {
            var   companyId = _stockManagementContext.Company.Where(x => x.CompanyName == orderdetails.Company.CompanyName.ToString()).Select(s => s.CompanyId).FirstOrDefault();
            Order order     = new Order()
            {
                CompanyId = companyId,
                Quantity  = orderdetails.Quantity,
                OrderDate = DateTime.Now,
                UserName  = orderdetails.UserName,
            };

            _stockManagementContext.Order.Add(order);
            _stockManagementContext.SaveChanges();
            return(order);
        }
Exemplo n.º 6
0
        public User Create(User user, string password)
        {
            // validation
            if (string.IsNullOrWhiteSpace(password))
            {
                throw new AppException("Password is required");
            }

            if (_context.Users.Any(x => x.Email == user.Email))
            {
                throw new AppException("Username \"" + user.Email + "\" is already taken");
            }

            byte[] passwordHash, passwordSalt;
            CreatePasswordHash(password, out passwordHash, out passwordSalt);

            user.PasswordHash = passwordHash;
            user.PasswordSalt = passwordSalt;

            _context.Users.Add(user);
            _context.SaveChanges();

            return(user);
        }
Exemplo n.º 7
0
        public async Task <Company> AddStock(int stockQuantity)
        {
            //To Generate Stock Price between 2-200 euros
            Random price      = new Random();
            var    stockPrice = price.Next(2, 200);

            if ((stockPrice * stockQuantity) >= 10000)
            {
                return(new Company());
            }
            //To generate random company name
            StringBuilder str_build = new StringBuilder();
            Random        random    = new Random();

            char letter;

            for (int i = 0; i < 5; i++)
            {
                double flt   = random.NextDouble();
                int    shift = Convert.ToInt32(Math.Floor(25 * flt));
                letter = Convert.ToChar(shift + 65);
                str_build.Append(letter);
            }

            //Adding Company object
            Company company = new Company()
            {
                CompanyName   = str_build.ToString(),
                StockQuantity = stockQuantity,
                Price         = stockPrice,
                Status        = (stockPrice >= 100)
            };

            _stockManagementContext.Add(company);
            _stockManagementContext.SaveChanges();

            return(company);
        }
 public ActionResult <Barcode> Post([FromBody] Barcode barcode)
 {
     _context.Barcodes.Add(barcode);
     _context.SaveChanges();
     return(barcode);
 }
Exemplo n.º 9
0
 public ActionResult <UserAccessRight> Post([FromBody] UserAccessRight userAccessRight)
 {
     _context.UserAccessRights.Add(userAccessRight);
     _context.SaveChanges();
     return(userAccessRight);
 }