Ejemplo n.º 1
0
        public async Task <IActionResult> CreateUnit(UnitForDetailDto unitForDetailDto)
        {
            if (await _context.Units.AnyAsync(a => a.Description == unitForDetailDto.Description))
            {
                return(BadRequest("Unit Exist"));
            }

            Unit unitToCreate = new Unit();

            unitToCreate.Description = unitForDetailDto.Description;

            _invRepo.Add <Unit>(unitToCreate);

            if (await _invRepo.SaveAll())
            {
                return(Ok());
            }

            throw new System.Exception($"Failed to Create unit on save");
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> UpdateUnit(int id, UnitForDetailDto unitForDetailDto)
        {
            var unitFromRepository = await _invRepo.Get <Unit>(id);

            if (unitFromRepository == null)
            {
                return(BadRequest("Unit not available"));
            }

            if (await _context.Units.AnyAsync(a => a.Description == unitForDetailDto.Description && a.Id != id))
            {
                return(BadRequest("Unit Exist"));
            }

            unitFromRepository.Description = unitForDetailDto.Description;


            if (await _invRepo.SaveAll())
            {
                return(NoContent());
            }

            throw new System.Exception($"Updating unit {id} failed on save");
        }