Exemplo n.º 1
0
        public async Task <HttpResponseMessage> InitiateComment([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 Comment initialization

                var comment = new Comment();
                comment.OwnerIndex = account.Id;
                comment.PostIndex  = parameters.PostIndex;
                comment.Content    = parameters.Content;
                comment.Created    = _timeService.DateTimeUtcToUnix(DateTime.UtcNow);

                //Add category record
                UnitOfWork.RepositoryComments.Insert(comment);

                // Save changes into database.
                await UnitOfWork.CommitAsync();

                #endregion

                return(Request.CreateResponse(HttpStatusCode.OK, comment));
            }
            catch (Exception exception)
            {
                _log.Error(exception.Message, exception);
                return(Request.CreateResponse(HttpStatusCode.InternalServerError));
            }
        }
Exemplo n.º 2
0
        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));
            }
        }