Beispiel #1
0
        public async Task <IActionResult> UpdateUser(int id, [FromBody] UserForDetailDto userForUpdate)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var currentUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            var userFromRepo = await _repo.GetUser(id);

            if (userFromRepo == null)
            {
                return(NotFound($"Could not find user with an ID of {id}"));
            }

            if (currentUserId != userFromRepo.Id)
            {
                return(Unauthorized());
            }

            _mapper.Map(userForUpdate, userFromRepo);

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

            throw new Exception($"Updating user {id} failed on save");
        }
Beispiel #2
0
        public async Task <IActionResult> CreateRestaurant([FromBody] RestaurantForRegisterDto restaurantForRegister)
        {
            var restaurant = _mapper.Map <Restaurant>(restaurantForRegister);

            _repo.Add(restaurant);

            var restaurantToReturn = _mapper.Map <RestaurantForReturnDto>(restaurant);

            if (await _repo.SaveAll())
            {
                return(CreatedAtRoute("GetRestaurant", new { id = restaurant.Id }, restaurantToReturn));
            }

            throw new Exception("Fail to created restaurant");
        }