public async Task <ActionResult <Models.Photo> > AddPhoto(IFormFile file) { var user = await _userRepository.GetUserByUserNameAsync(User.GetUsername()); var result = await _photoService.UploadPhotoAsync(file); if (result.Error != null) { return(BadRequest(result.Error.Message)); } var photo = new Entities.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 <Models.Photo>(photo))); } return(BadRequest("Error during adding photo")); }
// CONSTRUCTORS /// <summary> /// Initializes a new instance of <see cref="PhotoWrapper"/> /// </summary> /// <param name="photo"> /// An instance of <see cref="Entities.Photo"/> /// </param> public PhotoWrapper(Entities.Photo photo) { this.photo = photo; this.likeAmount = photo.Likes.Count(l => l.IsLiked); this.dislikeAmount = photo.Likes.Count - likeAmount; this.commentAmount = photo.Comments.Count; }
/// <summary> /// Determines if user liked current photo /// </summary> /// <param name="photo"> /// Rated photo /// </param> /// <param name="user"> /// User that probably sets the like /// </param> /// <returns> /// True if user liked photo, false — if disliked, null — if user does not set like /// </returns> public bool?HasLiked(Entities.Photo photo, Entities.User user) { Entities.PhotoLike like = TryGetUserLike(photo, user); return(like?.IsLiked); }
// METHODS /// <summary> /// Gets user like if it exists /// </summary> /// <param name="photo"> /// Rated photo /// </param> /// <param name="user"> /// User that probably sets the like /// </param> /// <returns> /// An instance of <see cref="Entities.PhotoLike"/> if user rated photo, otherwise — null /// </returns> public Entities.PhotoLike TryGetUserLike(Entities.Photo photo, Entities.User user) { return(context.PhotoLike.FirstOrDefault(l => l.Photo.Id == photo.Id && l.User.Id == user.Id)); }