Ejemplo n.º 1
0
        public async Task <IActionResult> UpdateGrocery(int userId, int id, GroceryForCreationDto groceryForUpdateDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var groceryFromRepo = await _repo.GetUniqueGrocery(id);

            _mapper.Map(groceryForUpdateDto, groceryFromRepo);

            if (await _repo.SaveAll())
            {
                return(CreatedAtRoute("GetGrocery", new { userId = userId }, groceryForUpdateDto));
            }

            throw new Exception($"Updating grocery lot {id} failed on save");
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> AddGrocery(int userId, GroceryForCreationDto groceryForCreationDto)
        {
            var creator = await _repo.GetUser(userId);

            if (creator.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var grocery = _mapper.Map <Grocery>(groceryForCreationDto);

            grocery.userId = userId;

            _repo.Add(grocery);

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

            throw new Exception("Creation of grocery count failed on save");
        }