コード例 #1
0
        public async Task <IActionResult> Account(string userId)
        {
            var userFromRepo = await _repository.GetUser(userId);

            var userToReturn  = Mapper.Map <UserDto>(userFromRepo);
            var placeFromRepo = userFromRepo.UserPlaces.Select(e => e.Place);
            var placeToReturn = Mapper.Map <List <PlaceFavouriteDto> >(placeFromRepo);

            foreach (var place in placeToReturn)
            {
                var cityId = await _repository.GetCity(place.CityId);

                place.CountryId = cityId.CountryId;
            }
            userToReturn.Places = placeToReturn;
            return(Ok(userToReturn));
        }
コード例 #2
0
        public async Task <IActionResult> AddToFavourite([FromBody] UserForFavouriteDto user, int placeId)
        {
            var userFromRepo = await _repository.GetUser(user.Id);

            if (userFromRepo.UserPlaces.Any(p => p.PlaceId == placeId))
            {
                var userPlaceToDelete = userFromRepo.UserPlaces.FirstOrDefault(p => p.PlaceId == placeId);
                userFromRepo.UserPlaces.Remove(userPlaceToDelete);
            }
            else
            {
                userFromRepo.UserPlaces.Add(new UserPlace
                {
                    UserId  = user.Id,
                    PlaceId = placeId
                });
            }
            if (!await _repository.Save())
            {
                throw new Exception("Failed");
            }
            return(NoContent());
        }