public async Task <HttpResponseMessage> UpdateCategory([FromUri] int index,
                                                               [FromBody] InitiateCategoryViewModel parameters)
        {
            try
            {
                #region Parameters validation

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

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

                #endregion

                #region Category search

                // Search the category.
                var categories = UnitOfWork.RepositoryCategories.Search();
                categories = categories.Where(x => x.Id == index);

                var category = await categories.FirstOrDefaultAsync();

                if (category == null)
                {
                    _log.Error($"Category (Id: {index}) is not found in database.");
                    return(Request.CreateErrorResponse(HttpStatusCode.NotFound, HttpMessages.CategoryNotFound));
                }

                #endregion

                #region Information update

                // Search unix system time.
                var unixSystemTime = _timeService.DateTimeUtcToUnix(DateTime.UtcNow);

                // Modify information.
                category.Name         = parameters.Name;
                category.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));
            }
        }
        public async Task <HttpResponseMessage> InitiateCategory([FromBody] InitiateCategoryViewModel parameters)
        {
            try
            {
                #region Parameters validation

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

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

                #endregion

                #region Account validate

                // Search account information attached in 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 duplicate check

                var findCategoryConditions = new SearchCategoryViewModel();
                findCategoryConditions.Name       = new TextSearch();
                findCategoryConditions.Name.Value = parameters.Name;
                findCategoryConditions.Name.Mode  = TextComparision.EqualIgnoreCase;

                // Category has been created before.
                var categories = UnitOfWork.RepositoryCategories.Search();
                var category   = await UnitOfWork.RepositoryCategories.Search(categories, findCategoryConditions).FirstOrDefaultAsync();

                if (category != null)
                {
                    _log.Error($"Category with name : {parameters.Name} has been created before.");
                    return(Request.CreateErrorResponse(HttpStatusCode.Conflict, HttpMessages.CategoryDuplicated));
                }

                #endregion

                #region Record initialization

                // Search current time on system.
                var systemTime = _timeService.DateTimeUtcToUnix(DateTime.UtcNow);

                // Search the id of requester.
                //Initiate new category
                category = new Category();
                category.CreatorIndex = account.Id;
                category.Created      = systemTime;
                category.Name         = parameters.Name;

                //Add category record
                UnitOfWork.RepositoryCategories.Insert(category);

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

                #endregion

                return(Request.CreateResponse(HttpStatusCode.Created, category));
            }
            catch (Exception exception)
            {
                _log.Error(exception.Message, exception);
                return(Request.CreateResponse(HttpStatusCode.InternalServerError));
            }
        }