public IEnumerable <Post> GetPostsByCategoryInterest(PostInterest interestCategory) { try { _logger.LogInformation("Get Post by By Category of Interest was called"); return(_ctx.Posts .Include(p => p.PostInterests) .Where(p => p.PostInterests.Contains(interestCategory)) .ToList()); } catch (Exception ex) { _logger.LogError($"Failed to get the requested Posts: {ex}"); return(null); } }
public async Task <IActionResult> Create([FromForm] CreatePostViewModel newPostData) { var thisUser = await userManager.GetUserAsync(HttpContext.User); if (ModelState.IsValid) { List <Interest> addedInterests = new List <Interest>(); if (!(newPostData.Interests[0] is null)) { List <Interest> interests = (List <Interest>)dataRepository.GetAllInterests(); foreach (Interest interest in interests) { if (newPostData.Interests[0].Contains(interest.InterestCategory)) { addedInterests.Add(interest); } } } DateTime timeStamp = DateTime.Now; Post theNewPost = new Post { User = thisUser, PostDate = timeStamp, PostText = newPostData.PostText, PostScope = (Scope)newPostData.PostScope }; dataRepository.AddEntity(theNewPost); if (dataRepository.SaveAll()) { logger.LogError("Ok new post was saved"); } ; Post savedPost = dataRepository.GetPostByTimeStamp(timeStamp); var image = newPostData.PostImage; if (image != null && image.Length > 0) { if (image.ContentType.Contains("png") || image.ContentType.Contains("jpg") || image.ContentType.Contains("jpeg")) { var filePath = env.WebRootPath + "\\postImages\\" + savedPost.PostID + '.' + image.ContentType.Split('/')[1]; using (var stream = new FileStream(filePath, FileMode.Create)) { await image.CopyToAsync(stream); } savedPost.PostImage = "/postImages/" + savedPost.PostID + '.' + image.ContentType.Split('/')[1]; } } foreach (Interest interest in addedInterests) { PostInterest postInterest = new PostInterest { Interest = interest, Post = savedPost }; dataRepository.AddEntity(postInterest); } if (dataRepository.SaveAll()) { logger.LogError("Ok relationship of post and interests was saved"); } ; return(Ok("Ok new post was saved")); } return(BadRequest("Something was missing")); }