public async Task <HttpResponseMessage> FindComments([FromBody] SearchCommentViewModel conditions) { try { #region Parameter validation // Conditions haven't been initialized. if (conditions == null) { conditions = new SearchCommentViewModel(); Validate(conditions); } // Parameters are invalid. if (!ModelState.IsValid) { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } #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 Search comments // Search comments by using specific conditions. var comments = UnitOfWork.RepositoryComments.Search(); comments = UnitOfWork.RepositoryComments.Search(comments, conditions); var searchResult = new SearchResult <IQueryable <Comment> >(); searchResult.Total = await comments.CountAsync(); searchResult.Records = UnitOfWork.RepositoryComments.Paginate(comments, conditions.Pagination); #endregion return(Request.CreateResponse(HttpStatusCode.OK, searchResult)); } catch (Exception exception) { _log.Error(exception.Message, exception); return(Request.CreateResponse(HttpStatusCode.InternalServerError)); } }
/// <summary> /// Search comments by using specific conditions. /// </summary> /// <param name="comments"></param> /// <param name="conditions"></param> /// <returns></returns> public IQueryable <Comment> Search(IQueryable <Comment> comments, SearchCommentViewModel conditions) { // Comment index is specified. if (conditions.Id != null) { comments = comments.Where(x => x.Id == conditions.Id.Value); } // Owner index is specified. if (conditions.OwnerIndex != null) { comments = comments.Where(x => x.OwnerId == conditions.OwnerIndex.Value); } // Post index is specified. if (conditions.PostIndex != null) { comments = comments.Where(x => x.PostId == conditions.PostIndex.Value); } // Content is specified. if (conditions.Content != null && !string.IsNullOrWhiteSpace(conditions.Content.Value)) { var szContent = conditions.Content; switch (szContent.Mode) { case TextComparision.Contain: comments = comments.Where(x => x.Content.Contains(szContent.Value)); break; case TextComparision.Equal: comments = comments.Where(x => x.Content.Equals(szContent.Value)); break; case TextComparision.EqualIgnoreCase: comments = comments.Where( x => x.Content.Equals(szContent.Value, StringComparison.InvariantCultureIgnoreCase)); break; case TextComparision.StartsWith: comments = comments.Where(x => x.Content.StartsWith(szContent.Value)); break; case TextComparision.StartsWithIgnoreCase: comments = comments.Where( x => x.Content.StartsWith(szContent.Value, StringComparison.InvariantCultureIgnoreCase)); break; case TextComparision.EndsWith: comments = comments.Where(x => x.Content.EndsWith(szContent.Value)); break; case TextComparision.EndsWithIgnoreCase: comments = comments.Where( x => x.Content.EndsWith(szContent.Value, StringComparison.InvariantCultureIgnoreCase)); break; default: comments = comments.Where(x => x.Content.ToLower().Contains(szContent.Value.ToLower())); break; } } // Created is specified. if (conditions.Created != null) { // Search created time. var created = conditions.Created; // From is specified. if (created.From != null) { comments = comments.Where(x => x.CreatedTime >= created.From.Value); } // To is specified. if (created.To != null) { comments = comments.Where(x => x.CreatedTime <= created.To.Value); } } // Last modified is specified. if (conditions.LastModified != null) { // Search created time. var lastModified = conditions.LastModified; // From is specified. if (lastModified.From != null) { comments = comments.Where(x => x.LastModifiedTime >= lastModified.From.Value); } // To is specified. if (lastModified.To != null) { comments = comments.Where(x => x.LastModifiedTime <= lastModified.To.Value); } } return(comments); }
public async Task <HttpResponseMessage> InitiateCommentReport( [FromBody] InitiateCommentReportViewModel parameters) { try { #region Parameters validation // Parameters haven't been initialized. if (parameters == null) { parameters = new InitiateCommentReportViewModel(); Validate(parameters); } // Parameters are invalid. if (!ModelState.IsValid) { return(Request.CreateResponse(HttpStatusCode.BadRequest, ModelState)); } #endregion #region Comment find // Comment find conditions build. var findCommentViewModel = new SearchCommentViewModel(); findCommentViewModel.Id = parameters.CommentIndex; // Search all comments from database. var comments = UnitOfWork.RepositoryComments.Search(); comments = UnitOfWork.RepositoryComments.Search(comments, findCommentViewModel); // Search the first matched comment. var comment = await comments.FirstOrDefaultAsync(); if (comment == null) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, HttpMessages.CommentNotFound)); } #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 initialization var commentReport = new CommentReport(); commentReport.CommentIndex = comment.Id; commentReport.CommentOwnerIndex = comment.OwnerIndex; commentReport.CommentReporterIndex = account.Id; commentReport.Body = comment.Content; commentReport.Reason = parameters.Reason; commentReport.Created = _timeService.DateTimeUtcToUnix(DateTime.UtcNow); // Save record into database. commentReport = UnitOfWork.RepositoryCommentReports.Insert(commentReport); // Commit changes to database. await UnitOfWork.CommitAsync(); #endregion return(Request.CreateResponse(HttpStatusCode.OK, commentReport)); } catch (Exception exception) { _log.Error(exception.Message, exception); return(Request.CreateResponse(HttpStatusCode.InternalServerError)); } }
public async Task <HttpResponseMessage> FindCommentsDetails( [FromBody] FindCommentsDetailsViewModel searchCommentsDetailsCondition) { try { #region Parameter validation // Conditions haven't been initialized. if (searchCommentsDetailsCondition == null) { searchCommentsDetailsCondition = new FindCommentsDetailsViewModel(); Validate(searchCommentsDetailsCondition); } // Parameters are invalid. if (!ModelState.IsValid) { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } #endregion #region Search comments // Search categories by using specific conditions. var conditions = new SearchCommentViewModel(); conditions.Id = searchCommentsDetailsCondition.Id; conditions.OwnerIndex = searchCommentsDetailsCondition.OwnerIndex; conditions.PostIndex = searchCommentsDetailsCondition.PostIndex; conditions.Content = searchCommentsDetailsCondition.Content; conditions.Created = searchCommentsDetailsCondition.Created; conditions.Direction = searchCommentsDetailsCondition.Direction; conditions.LastModified = searchCommentsDetailsCondition.LastModified; conditions.Pagination = searchCommentsDetailsCondition.Pagination; var comments = UnitOfWork.RepositoryComments.Search(); comments = UnitOfWork.RepositoryComments.Search(comments, conditions); // Search accounts from database. var accounts = UnitOfWork.RepositoryAccounts.Search(); // Search comments details var commentsDetails = from comment in comments from account in accounts where comment.OwnerIndex == account.Id select new CommentDetailViewModel { Id = comment.Id, Owner = account, Content = comment.Content, Created = comment.Created, LastModified = comment.LastModified }; #endregion #region Comments Details sort commentsDetails = UnitOfWork.RepositoryComments.Sort(commentsDetails, searchCommentsDetailsCondition.Direction, searchCommentsDetailsCondition.Sort); #endregion #region Pagination var searchResult = new SearchResult <IQueryable <CommentDetailViewModel> >(); searchResult.Total = await commentsDetails.CountAsync(); searchResult.Records = UnitOfWork.RepositoryComments.Paginate(commentsDetails, conditions.Pagination); #endregion return(Request.CreateResponse(HttpStatusCode.OK, searchResult)); } catch (Exception exception) { _log.Error(exception.Message, exception); return(Request.CreateResponse(HttpStatusCode.InternalServerError)); } }
public async Task <HttpResponseMessage> DeleteComments([FromBody] SearchCommentViewModel conditions) { try { #region Parameters validation // Conditions haven't been initialized. if (conditions == null) { conditions = new SearchCommentViewModel(); Validate(conditions); } //Request 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 Search & delete records // Account can only delete its own comment as it is not an administrator. if (account.Role != Roles.Admin) { conditions.OwnerIndex = account.Id; } // Search all comments in database. var comments = UnitOfWork.RepositoryComments.Search(); comments = UnitOfWork.RepositoryComments.Search(comments, conditions); // Search comment reports and delete them first. var commentReports = UnitOfWork.RepositoryCommentReports.Search(); var markCommentReports = from comment in comments from commentReport in commentReports where comment.Id == commentReport.CommentIndex select commentReport; // Delete all found comments. UnitOfWork.RepositoryCommentReports.Remove(markCommentReports); UnitOfWork.RepositoryComments.Remove(comments); // Save changes. var totalRecords = await UnitOfWork.CommitAsync(); // No record has been deleted. if (totalRecords < 1) { _log.Error($"There is no comment (Id: {conditions.Id}) from database."); return(Request.CreateErrorResponse(HttpStatusCode.NotFound, HttpMessages.CommentNotFound)); } #endregion // Tell the client , deletion is successful. return(Request.CreateResponse(HttpStatusCode.OK)); } catch (Exception exception) { _log.Error(exception.Message, exception); return(Request.CreateResponse(HttpStatusCode.InternalServerError)); } }
public async Task <HttpResponseMessage> UpdateComment([FromUri] int index, [FromBody] InitiateCommentViewModel parameters) { try { #region Parameters validation // Parameters haven't been initialized. if (parameters == null) { parameters = new InitiateCommentViewModel(); Validate(parameters); } //Request 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 find // Search the category var condition = new SearchCommentViewModel(); condition.Id = index; // Account can only change its comment as it is not an administrator. if (account.Role != Roles.Admin) { condition.OwnerIndex = account.Id; } // Search categories by using specific conditions. var comments = UnitOfWork.RepositoryComments.Search(); comments = UnitOfWork.RepositoryComments.Search(comments, condition); #endregion #region Record update // Calculate the system time. var unixSystemTime = _timeService.DateTimeUtcToUnix(DateTime.UtcNow); // Update all categories. foreach (var comment in comments) { comment.Content = parameters.Content; comment.LastModified = unixSystemTime; } // Save changes into database. await UnitOfWork.CommitAsync(); #endregion return(Request.CreateResponse(HttpStatusCode.OK)); } catch (Exception exception) { _log.Error(exception.Message, exception); return(Request.CreateResponse(HttpStatusCode.InternalServerError)); } }