public bool ResourceDepartureCheck(ResourceMovimentation data)
        {
            try
            {
                Resource      res              = _context.Resources.FirstOrDefault(x => x.Id == data.ResourceId);
                ResourceStock stock            = _context.ResourceStocks.FirstOrDefault(x => x.ResourceId == data.ResourceId);
                int           newQuantityValue = stock.Quantity - data.Quantity;
                if (newQuantityValue < res.MinQuantity)
                {
                    return(false);
                }
                else if (newQuantityValue == res.MinQuantity)
                {
                    res.Status = false;
                    _context.Resources.Update(res);
                    _context.SaveChanges();

                    return(true);
                }
                else
                {
                    return(true);
                }
            }
            catch (System.Exception)
            {
                throw;
            }
        }
Exemplo n.º 2
0
        public IActionResult ResourceDepartureInsert([FromHeader] string Token, [FromBody] ResourceMovimentation data)
        {
            try
            {
                UserDataHandler authUser = new AuthService(_context).GetUserByToken(Token);

                if (authUser != null)
                {
                    bool departureCheck = new ResourcesService(_context).ResourceDepartureCheck(data);
                    if (departureCheck)
                    {
                        new ResourcesService(_context).InsertResourceDeparture(data, authUser.Id);

                        return(Ok("Saída de recurso cadastrada com sucesso."));
                    }
                    else
                    {
                        return(BadRequest("Você não pode retirar essa quantidade!"));
                    }
                }
                else
                {
                    return(Unauthorized("Token inválido."));
                }
            }
            catch (System.Exception)
            {
                throw;
            }
        }
        public void InsertResourceDeparture(ResourceMovimentation data, int userId)
        {
            try
            {
                ResourceDeparture resDeparture = new ResourceDeparture
                {
                    ResourceId     = data.ResourceId,
                    Quantity       = data.Quantity,
                    CreationUserId = userId,
                    DepartureDate  = DateTime.Now
                };

                _context.ResourceDepartures.Add(resDeparture);
                _context.SaveChanges();
            }
            catch (System.Exception)
            {
                throw;
            }
        }
        public void InsertResourceEntry(ResourceMovimentation data, int userId)
        {
            try
            {
                ResourceEntry resEntry = new ResourceEntry
                {
                    ResourceId     = data.ResourceId,
                    Quantity       = data.Quantity,
                    CreationUserId = userId,
                    CreationDate   = DateTime.Now
                };

                _context.ResourceEntries.Add(resEntry);
                _context.SaveChanges();
            }
            catch (System.Exception)
            {
                throw;
            }
        }