Ejemplo n.º 1
0
        public async Task <IActionResult> AddActivity(int userId, ActivityForCreationDto activityForCreationDto)
        {
            //            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            //                return Unauthorized();
            var userFromRepo = await _repository.GetUser(userId);

            var activity = _mapper.Map <Activity> (activityForCreationDto);

            userFromRepo.Activities.Add(activity);

            if (await _repository.SaveAll())
            {
                var activityToReturn = _mapper.Map <ActivityForReturnDto> (activity);
                return(CreatedAtRoute("GetActivity", new { id = activity.Id }, activityToReturn));
            }

            return(BadRequest("Could not add the activity"));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> SetMainPhoto(int userId, int id)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var user = await _repository.GetUser(userId);

            if (!user.Photos.Any(p => p.Id == id))
            {
                return(Unauthorized());
            }

            var photoFromRepo = await _repository.GetPhoto(id);

            if (photoFromRepo.IsMain)
            {
                return(BadRequest("This is already the main photo"));
            }

            var currentMainPhoto = await _repository.GetMainPhotoForUser(userId);

            if (currentMainPhoto == null)
            {
                return(BadRequest("There is an error with the resources related with the user"));
            }

            currentMainPhoto.IsMain = false;

            photoFromRepo.IsMain = true;

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

            return(BadRequest("Could not set photo to main"));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> UpdateUser(int id, UserForUpdateDto userForUpdateDto)
        {
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var userFromRepo = await _repository.GetUser(id);

            _mapper.Map(userForUpdateDto, userFromRepo);

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

            throw new Exception($"Updating user {id} failed on update");
        }