public async Task <HttpResponseMessage> FindCommentReports([FromBody] SearchCommentReportViewModel condition) { try { #region Parameters validation // Parameters haven't been initialized. if (condition == null) { condition = new SearchCommentReportViewModel(); Validate(condition); } // Parameters are invalid. if (!ModelState.IsValid) { return(Request.CreateResponse(HttpStatusCode.BadRequest, FindValidationMessage(ModelState, nameof(condition)))); } #endregion #region Request identity search // Search account which sends the current request. var account = _identityService.FindAccount(Request.Properties); if (account == null) { throw new Exception("No account information is attached into current request."); } #endregion #region Comment report search // Account can only see the comments which it is their reporter. if (account.Role != Roles.Admin) { condition.CommentReporterIndex = account.Id; } // Search comment reports with specific conditions. var commentReports = UnitOfWork.RepositoryCommentReports.Search(); commentReports = UnitOfWork.RepositoryCommentReports.Search(commentReports, condition); commentReports = UnitOfWork.RepositoryCommentReports.Sort(commentReports, condition.Direction, condition.Sort); // Search all accounts. var accounts = UnitOfWork.RepositoryAccounts.Search(); // Search all comments. var comments = UnitOfWork.RepositoryComments.Search(); // Search all posts. var posts = UnitOfWork.RepositoryPosts.Search(); // Search comment report details var commentReportDetails = from commentReport in commentReports from commentOwner in accounts from commentReporter in accounts from comment in comments from post in posts where (commentReport.CommentOwnerIndex == commentOwner.Id) && (commentReport.CommentReporterIndex == commentReporter.Id) && (commentReport.PostIndex == post.Id) && (commentReport.CommentIndex == comment.Id) select new CommentReportDetailViewModel { Comment = comment, Post = post, Body = commentReport.Body, Reason = commentReport.Reason, Created = commentReport.Created }; #endregion #region Comment report search // Initiate result. var result = new SearchResult <IQueryable <CommentReportDetailViewModel> >(); // Count the total records first. result.Total = await commentReports.CountAsync(); // Do pagination. result.Records = UnitOfWork.RepositoryCommentReports.Paginate(commentReportDetails, condition.Pagination); #endregion return(Request.CreateResponse(HttpStatusCode.OK, result)); } catch (Exception exception) { _log.Error(exception.Message, exception); return(Request.CreateResponse(HttpStatusCode.InternalServerError)); } }
/// <summary> /// Search comment reports by using specific conditions. /// </summary> /// <param name="commentReports"></param> /// <param name="conditions"></param> /// <returns></returns> public IQueryable <CommentReport> Search(IQueryable <CommentReport> commentReports, SearchCommentReportViewModel conditions) { // Comment index is specified. if (conditions.CommentIndex != null) { commentReports = commentReports.Where(x => x.CommentId == conditions.CommentIndex.Value); } // Comment owner is specified. if (conditions.CommentOwnerIndex != null) { commentReports = commentReports.Where(x => x.CommentId == conditions.CommentOwnerIndex.Value); } // Reporter index is specified. if (conditions.CommentReporterIndex != null) { commentReports = commentReports.Where(x => x.ReporterId == conditions.CommentReporterIndex.Value); } // Comment body is specified. if (conditions.Body != null && !string.IsNullOrEmpty(conditions.Body.Value)) { var szBody = conditions.Body; switch (szBody.Mode) { case TextComparision.Contain: commentReports = commentReports.Where(x => x.Body.Contains(szBody.Value)); break; case TextComparision.Equal: commentReports = commentReports.Where(x => x.Body.Equals(szBody.Value)); break; case TextComparision.EqualIgnoreCase: commentReports = commentReports.Where( x => x.Body.Equals(szBody.Value, StringComparison.InvariantCultureIgnoreCase)); break; case TextComparision.StartsWith: commentReports = commentReports.Where(x => x.Body.StartsWith(szBody.Value)); break; case TextComparision.StartsWithIgnoreCase: commentReports = commentReports.Where( x => x.Body.StartsWith(szBody.Value, StringComparison.InvariantCultureIgnoreCase)); break; case TextComparision.EndsWith: commentReports = commentReports.Where(x => x.Body.EndsWith(szBody.Value)); break; case TextComparision.EndsWithIgnoreCase: commentReports = commentReports.Where( x => x.Body.EndsWith(szBody.Value, StringComparison.InvariantCultureIgnoreCase)); break; default: commentReports = commentReports.Where(x => x.Body.ToLower().Contains(szBody.Value.ToLower())); break; } return(commentReports); } // Comment reason is specified. if (conditions.Reason != null && !string.IsNullOrEmpty(conditions.Reason.Value)) { var szReason = conditions.Reason; switch (szReason.Mode) { case TextComparision.Contain: commentReports = commentReports.Where(x => x.Reason.Contains(szReason.Value)); break; case TextComparision.Equal: commentReports = commentReports.Where(x => x.Reason.Equals(szReason.Value)); break; case TextComparision.EqualIgnoreCase: commentReports = commentReports.Where( x => x.Reason.Equals(szReason.Value, StringComparison.InvariantCultureIgnoreCase)); break; case TextComparision.StartsWith: commentReports = commentReports.Where(x => x.Reason.StartsWith(szReason.Value)); break; case TextComparision.StartsWithIgnoreCase: commentReports = commentReports.Where( x => x.Reason.StartsWith(szReason.Value, StringComparison.InvariantCultureIgnoreCase)); break; case TextComparision.EndsWith: commentReports = commentReports.Where(x => x.Reason.EndsWith(szReason.Value)); break; case TextComparision.EndsWithIgnoreCase: commentReports = commentReports.Where( x => x.Reason.EndsWith(szReason.Value, StringComparison.InvariantCultureIgnoreCase)); break; default: commentReports = commentReports.Where(x => x.Reason.ToLower().Contains(szReason.Value.ToLower())); break; } } // Created is specified. if (conditions.Created != null) { // Comment created time. var created = conditions.Created; // From is defined. if (created.From != null) { commentReports = commentReports.Where(x => x.CreatedTime >= created.From.Value); } // To is defined. if (created.To != null) { commentReports = commentReports.Where(x => x.CreatedTime <= created.To.Value); } } return(commentReports); }
public async Task <HttpResponseMessage> DeleteCommentReport([FromBody] SearchCommentReportViewModel parameters) { try { #region Parameters validation // Parameters haven't been initialized. if (parameters == null) { parameters = new SearchCommentReportViewModel(); Validate(parameters); } // Parameters are invalid. if (!ModelState.IsValid) { return(Request.CreateResponse(HttpStatusCode.BadRequest, ModelState)); } #endregion #region Request identity search // Search account which sends the current request. var account = _identityService.FindAccount(Request.Properties); if (account == null) { throw new Exception("No account information is attached into current request."); } #endregion #region Record delete // Account can only delete the reports whose reporter is the requester. if (account.Role != Roles.Admin) { parameters.CommentReporterIndex = account.Id; } // Search comment reports by using specific conditions. var commentReports = UnitOfWork.RepositoryCommentReports.Search(); commentReports = UnitOfWork.RepositoryCommentReports.Search(commentReports, parameters); // Search comments and delete 'em all. UnitOfWork.RepositoryCommentReports.Remove(commentReports); // Save changes. var totalRecords = await UnitOfWork.CommitAsync(); // Nothing is changed. if (totalRecords < 1) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, HttpMessages.CommentReportNotFound)); } #endregion return(Request.CreateResponse(HttpStatusCode.OK)); } catch (Exception exception) { _log.Error(exception.Message, exception); return(Request.CreateResponse(HttpStatusCode.InternalServerError)); } }