Exemplo n.º 1
0
        public async Task <IActionResult> Post([FromBody] AddProductInputModel input)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(GetErrorListFromModelState.GetErrorList(ModelState)));
            }
            if (!TypesManager.checkStatus(input.Type))
            {
                return(BadRequest(new { errors = new { Type = ErrorConst.InvalidType } }));
            }
            var product = new Product
            {
                Name        = input.Name,
                Description = input.Description,
                Type        = input.Type,
                OwnerId     = input.OwnerId
            };

            _context.Products.Add(product);
            try
            {
                await _context.SaveChangesAsync();

                return(Ok());
            }
            catch (Exception ex)
            {
                return(GetCatchExceptionErrors.getErrors(this, ex));
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Delete([FromQuery][Required(ErrorMessage = ErrorConst.REQUIRED)] Guid id)
        {
            if (ModelState.IsValid)
            {
                Product product = _context.Products.FirstOrDefault(c => c.Id == id);
                if (product != null)
                {
                    try
                    {
                        _context.Products.Remove(product);
                        await _context.SaveChangesAsync();

                        return(Ok());
                    }
                    catch (Exception ex)
                    {
                        return(GetCatchExceptionErrors.getErrors(this, ex));
                    }
                }
                else
                {
                    return(BadRequest(new { errors = new { id = ErrorConst.NO_ITEM } }));
                }
            }
            else
            {
                return(BadRequest(GetErrorListFromModelState.GetErrorList(ModelState)));
            }
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Put([FromBody] EditProductInputModel input)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(GetErrorListFromModelState.GetErrorList(ModelState)));
            }
            if (!TypesManager.checkStatus(input.Type))
            {
                return(BadRequest(new { errors = new { Type = ErrorConst.InvalidType } }));
            }

            var product = _context.Products.Where(i => i.Id == input.Id).FirstOrDefault();

            if (product != null)
            {
                try
                {
                    product.Name        = input.Name;
                    product.Description = input.Description;
                    product.Type        = input.Type;
                    product.OwnerId     = input.OwnerId;
                    _context.Products.Update(product);
                    await _context.SaveChangesAsync();

                    return(Ok());
                }
                catch (Exception ex)
                {
                    return(GetCatchExceptionErrors.getErrors(this, ex));
                }
            }
            else
            {
                return(BadRequest(new { errors = new { Id = ErrorConst.NO_ITEM } }));
            }
        }