Пример #1
0
        public async Task <ActionResult> SetMainPhoto(int photoId)
        {
            var user = await _userRepository.GetUserByUsernameAsync(User.getUsername());

            var photo = user.Photos.FirstOrDefault(x => x.Id == photoId);

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

            var currentMain = user.Photos.FirstOrDefault(x => x.IsMain);

            if (currentMain != null)
            {
                currentMain.IsMain = false;
            }
            photo.IsMain = true;

            if (await _userRepository.SaveAllAsync())
            {
                return(NoContent());
            }

            return(BadRequest("Failed to set photo!"));
        }
Пример #2
0
        public async Task <ActionResult> DeletePhoto(int photoId)
        {
            var user = await _userRepository.GetUserByUsernameAsync(User.getUsername());

            var photo = user.Photos.FirstOrDefault(x => x.Id == photoId);

            if (photo == null)
            {
                return(NotFound());
            }

            if (photo.IsMain)
            {
                return(BadRequest("You cannot delete your main photo"));
            }

            if (photo.PublicId != null)
            {
                var result = await _photoService.DeletePhotoAsync(photo.PublicId);

                if (result.Error != null)
                {
                    return(BadRequest(result.Error.Message));
                }
            }
            user.Photos.Remove(photo);

            if (await _userRepository.SaveAllAsync())
            {
                return(Ok());
            }

            return(BadRequest("Something went wrong with deleting the photo"));
        }
Пример #3
0
        public async Task <ActionResult> DeleteMessage(int id)
        {
            var username = User.getUsername();
            var message  = await _messageRepository.GetMessage(id);

            if (message.Sender.UserName != username && message.Recepient.UserName != username)
            {
                return(Unauthorized());
            }

            if (message.Sender.UserName == username)
            {
                message.SenderDeleted = true;
            }

            if (message.Recepient.UserName == username)
            {
                message.RecepientDeleted = true;
            }

            if (message.SenderDeleted && message.RecepientDeleted)
            {
                _messageRepository.DeleteMessage(message);
            }

            if (await _messageRepository.SaveAllAsync())
            {
                return(Ok());
            }

            return(BadRequest("Problem Deleting message"));
        }
Пример #4
0
        public async Task <ActionResult <PhotoDto> > AddPhoto(IFormFile file)
        {
            var user = await _userRepository.GetUserByUsernameAsync(User.getUsername());

            var result = await _photoService.AddPhotoAsync(file);

            if (result.Error != null)
            {
                return(BadRequest(result.Error.Message));
            }

            var photo = new Photo
            {
                Url      = result.SecureUrl.AbsoluteUri,
                PublicId = result.PublicId
            };

            if (user.Photos.Count == 0)
            {
                photo.IsMain = true;
            }

            user.Photos.Add(photo);

            if (await _userRepository.SaveAllAsync())
            {
                return(CreatedAtRoute("GetUser", new { username = user.UserName }, _mapper.Map <PhotoDto>(photo)));
            }
            return(BadRequest("Problem Adding Photo"));
        }
Пример #5
0
        public async Task <ActionResult <IEnumerable <MessageDto> > > GetMessagesForUser([FromQuery]
                                                                                         MessageParams messageParams)
        {
            messageParams.Username = User.getUsername();

            var messages = await _messageRepository.GetMessagesForUser(messageParams);

            Response.AddPaginationHeader(messages.CurrentPage, messages.Count, messages.TotalCount, messages.TotalPages);

            return(messages);
        }
Пример #6
0
        public async Task <ActionResult> UpdateUser(MemberUpdateDto memberUpdateDto)
        {
            var user = await _userRepository.GetUserByUsernameAsync(User.getUsername());

            _mapper.Map(memberUpdateDto, user); //iz memberupdatedto bo vse prepisalo na userja namesto da za vsak property delaš ročno

            _userRepository.Update(user);

            if (await _userRepository.SaveAllAsync())
            {
                return(NoContent());
            }

            return(BadRequest("Failed to update user"));
        }
Пример #7
0
        public async Task <ActionResult> UpdateUser(MemberUpdateDto memberUpdateDto)
        {
            var user = await _userRepository.GetUserByUsernameAsync(User.getUsername());

            _mapper.Map(memberUpdateDto, user);

            _userRepository.Update(user);

            if (await _userRepository.SaveAllAsync())
            {
                return(NoContent());
            }

            return(BadRequest("Failed to update User"));
        }
Пример #8
0
        public async Task <ActionResult <IEnumerable <MemberDto> > > GetUsers([FromQuery] UserParams userParams)
        {
            var user = await _userRepository.GetUserByUsernameAsync(User.getUsername());

            userParams.CurrentUsername = user.UserName;

            if (string.IsNullOrEmpty(userParams.Country))
            {
                userParams.Country = user.Country;
            }


            var users = await _userRepository.GetMembersAsync(userParams);

            Response.AddPaginationHeader(users.CurrentPage, users.PageSize,
                                         users.TotalCount, users.TotalPages);

            return(Ok(users));
        }
Пример #9
0
        public async Task <ActionResult <MessageDto> > CreateMessage(CreateMessageDto createMessageDto)
        {
            var username = User.getUsername();

            if (username.ToLower() == createMessageDto.RecepientUsername.ToLower())
            {
                return(BadRequest("You cannot message yourself"));
            }

            var sender = await _userRepository.GetUserByUsernameAsync(username);

            var recepient = await _userRepository.GetUserByUsernameAsync(createMessageDto.RecepientUsername);

            if (recepient == null)
            {
                return(NotFound());
            }

            var message = new Message
            {
                Sender            = sender,
                Recepient         = recepient,
                SenderUsername    = sender.UserName,
                RecepientUsername = recepient.UserName,
                Content           = createMessageDto.Content
            };

            _messageRepository.AddMessage(message);

            if (await _messageRepository.SaveAllAsync())
            {
                return(Ok(_mapper.Map <MessageDto>(message)));
            }

            return(BadRequest("Failed to send message"));
        }
Пример #10
0
        public async Task <ActionResult <PhotoDto> > AddPhoto(IFormFile file)
        {
            var user = await _userRepository.GetUserByUsernameAsync(User.getUsername());

            var result = await _photoService.AddPhotoAsync(file);

            if (result.Error != null)
            {
                return(BadRequest(result.Error.Message));
            }

            DeleteOldPhoto(user);

            var photo = CreatePhoto(result);

            AddPhotoToDb(photo, user);

            if (await _userRepository.SaveAllAsync())
            {
                return(CreatedAtRoute("GetUser", new { username = user.UserName }, _mapper.Map <PhotoDto>(photo)));
            }

            return(BadRequest("problem adding photo"));
        }
Пример #11
0
        public async Task <ActionResult <IEnumerable <MessageDto> > > GetMessageThread(string username)
        {
            var currentUsername = User.getUsername();

            return(Ok(await _messageRepository.GetMessageThread(currentUsername, username)));
        }