Example #1
0
        public async Task <HttpResponseMessage> DeletePostReports([FromBody] SearchPostReportViewModel parameters)
        {
            try
            {
                #region Parameters validation

                // Parameters haven't been initialized.
                if (parameters == null)
                {
                    // Initiate parameters.
                    parameters = new SearchPostReportViewModel();
                    Validate(parameters);
                }

                // Parameters are invalid.
                if (!ModelState.IsValid)
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, ModelState));
                }

                #endregion

                #region Record delete

                // Search post reports.
                var postReports = UnitOfWork.RepositoryPostReports.Search();
                postReports = UnitOfWork.RepositoryPostReports.Search(postReports, parameters);

                // Find comment
                // Delete post reports by using specific conditions.
                UnitOfWork.RepositoryPostReports.Remove(postReports);

                // Commit the change.
                var totalRecord = await UnitOfWork.CommitAsync();

                // No record has been affected.
                if (totalRecord < 1)
                {
                    _log.Error($"No post report (ID: {parameters.Id}) is found in database");
                    return(Request.CreateErrorResponse(HttpStatusCode.NotFound, HttpMessages.PostReportNotFound));
                }

                #endregion

                return(Request.CreateResponse(HttpStatusCode.OK));
            }
            catch (Exception exception)
            {
                _log.Error(exception.Message, exception);
                return(Request.CreateResponse(HttpStatusCode.InternalServerError));
            }
        }
Example #2
0
        public async Task <HttpResponseMessage> FindPostReports([FromBody] SearchPostReportViewModel parameters)
        {
            try
            {
                #region Parameters validation

                // Parameters haven't been initialized.
                if (parameters == null)
                {
                    // Initiate parameters.
                    parameters = new SearchPostReportViewModel();
                    Validate(parameters);
                }

                // Parameters are invalid.
                if (!ModelState.IsValid)
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, ModelState));
                }

                #endregion

                #region Search post reports

                // Search all post reports.
                var postReports = UnitOfWork.RepositoryPostReports.Search();
                postReports = UnitOfWork.RepositoryPostReports.Search(postReports, parameters);

                // Search posts from database.
                var posts = UnitOfWork.RepositoryPosts.Search();

                // Search all accounts.
                var accounts = UnitOfWork.RepositoryAccounts.Search();

                // Search post report by using specific conditions.
                var postReportDetails = from postReport in postReports
                                        from post in posts
                                        from owner in accounts
                                        from reporter in accounts
                                        where postReport.PostIndex == post.Id &&
                                        postReport.PostOwnerIndex == owner.Id &&
                                        postReport.PostReporterIndex == reporter.Id
                                        select new PostReportViewModel
                {
                    Post     = post,
                    Owner    = owner,
                    Reporter = reporter,
                    Body     = postReport.Body,
                    Reason   = postReport.Reason,
                    Created  = postReport.Created
                };

                // Order records.
                postReportDetails = UnitOfWork.RepositoryPostReports.Sort(postReportDetails, parameters.Direction, parameters.Sort);

                // Initiate search result
                var searchResult = new SearchResult <IQueryable <PostReportViewModel> >();
                searchResult.Total = await postReportDetails.CountAsync();

                searchResult.Records = UnitOfWork.RepositoryPostReports.Paginate(postReportDetails, parameters.Pagination);

                #endregion

                return(Request.CreateResponse(HttpStatusCode.OK, searchResult));
            }
            catch (Exception exception)
            {
                _log.Error(exception.Message, exception);
                return(Request.CreateResponse(HttpStatusCode.InternalServerError));
            }
        }
Example #3
0
        /// <summary>
        ///     Search posts by using specific conditions.
        /// </summary>
        /// <param name="postReports"></param>
        /// <param name="conditions"></param>
        /// <returns></returns>
        public IQueryable <PostReport> Search(IQueryable <PostReport> postReports,
                                              SearchPostReportViewModel conditions)
        {
            // Owner index is specified.
            if (conditions.PostOwnerIndex != null)
            {
                postReports = postReports.Where(x => x.OwnerId == conditions.PostOwnerIndex.Value);
            }

            // Reporter index is specified.
            if (conditions.PostReporterIndex != null)
            {
                postReports = postReports.Where(x => x.ReporterId == conditions.PostReporterIndex.Value);
            }

            // Body of post.
            if (conditions.Body != null && !string.IsNullOrEmpty(conditions.Body.Value))
            {
                var szBody = conditions.Body;
                switch (szBody.Mode)
                {
                case TextSearchMode.Contain:
                    postReports = postReports.Where(x => x.Body.Contains(szBody.Value));
                    break;

                case TextSearchMode.Equal:
                    postReports = postReports.Where(x => x.Body.Equals(szBody.Value));
                    break;

                case TextSearchMode.EqualIgnoreCase:
                    postReports =
                        postReports.Where(
                            x => x.Body.Equals(szBody.Value, StringComparison.CurrentCultureIgnoreCase));
                    break;

                case TextSearchMode.StartsWith:
                    postReports = postReports.Where(x => x.Body.StartsWith(szBody.Value));
                    break;

                case TextSearchMode.StartsWithIgnoreCase:
                    postReports =
                        postReports.Where(
                            x => x.Body.StartsWith(szBody.Value, StringComparison.CurrentCultureIgnoreCase));
                    break;

                case TextSearchMode.EndsWith:
                    postReports = postReports.Where(x => x.Body.EndsWith(szBody.Value));
                    break;

                case TextSearchMode.EndsWithIgnoreCase:
                    postReports =
                        postReports.Where(
                            x => x.Body.EndsWith(szBody.Value, StringComparison.CurrentCultureIgnoreCase));
                    break;

                default:
                    postReports = postReports.Where(x => x.Body.ToLower().Contains(szBody.Value.ToLower()));
                    break;
                }
            }
            // Reason is specified.
            if (conditions.Reason != null && !string.IsNullOrEmpty(conditions.Reason.Value))
            {
                var szReason = conditions.Reason;
                switch (szReason.Mode)
                {
                case TextSearchMode.Contain:
                    postReports = postReports.Where(x => x.Reason.Contains(szReason.Value));
                    break;

                case TextSearchMode.Equal:
                    postReports = postReports.Where(x => x.Reason.Equals(szReason.Value));
                    break;

                case TextSearchMode.EqualIgnoreCase:
                    postReports =
                        postReports.Where(
                            x => x.Reason.Equals(szReason.Value, StringComparison.CurrentCultureIgnoreCase));
                    break;

                case TextSearchMode.StartsWith:
                    postReports = postReports.Where(x => x.Reason.StartsWith(szReason.Value));
                    break;

                case TextSearchMode.StartsWithIgnoreCase:
                    postReports =
                        postReports.Where(
                            x => x.Reason.StartsWith(szReason.Value, StringComparison.CurrentCultureIgnoreCase));
                    break;

                case TextSearchMode.EndsWith:
                    postReports = postReports.Where(x => x.Reason.EndsWith(szReason.Value));
                    break;

                case TextSearchMode.EndsWithIgnoreCase:
                    postReports =
                        postReports.Where(
                            x => x.Reason.EndsWith(szReason.Value, StringComparison.CurrentCultureIgnoreCase));
                    break;

                default:
                    postReports = postReports.Where(x => x.Reason.ToLower().Contains(szReason.Value.ToLower()));
                    break;
                }
            }

            //// CreatedTime is specified.
            //if (conditions.CreatedTime != null)
            //{
            //    var created = conditions.CreatedTime;

            //    // From is defined.
            //    if (created.From != null)
            //        postReports = postReports.Where(x => x.Created >= created.From.Value);

            //    // To is defined.
            //    if (created.To != null)
            //        postReports = postReports.Where(x => x.Created <= created.To.Value);
            //}

            return(postReports);
        }