Exemple #1
0
 public Task <IActionResult> Add(Content model)
 {
     return(Task.Factory.StartNew <IActionResult>(() =>
     {
         if (!ModelState.IsValid)
         {
             return Json(ExcutedResult.FailedResult("数据验证失败"));
         }
         ContentRepository.AddAsync(model, false);
         return Json(ExcutedResult.SuccessResult());
     }));
 }
Exemple #2
0
 public IActionResult Post([FromBody] Content c)
 {
     try
     {
         if (c == null || !ModelState.IsValid)
         {
             return(BadRequest(ErrorCode.TitleAndContentRequired.ToString()));
         }
         bool itemExists = _repository.DoesItemExist(c.ContentId).Result;
         if (itemExists)
         {
             return(StatusCode(StatusCodes.Status409Conflict, ErrorCode.IDInUse.ToString()));
         }
         _repository.AddAsync(c);
     }
     catch (Exception)
     {
         return(BadRequest(ErrorCode.CouldNotCreateItem.ToString()));
     }
     return(Ok(c));
 }
        public async Task <MContentResult <ContentDto> > AddAsync(ContentDto contentDto)
        {
            //Validate
            var validationResult = await _validationService.ValidateAdd(contentDto);

            if (!validationResult.IsValid)
            {
                return(validationResult.ConvertFromValidationResult <ContentDto>());
            }
            //Map to domain object
            var entityForDb = _mapper.Map <Content>(contentDto);
            var addedEntity = await _repository.AddAsync(entityForDb);

            var retrievedEntity = await _repository.GetByIdAsync(addedEntity.Id);

            //Map to dto
            var resultEntity = _mapper.Map <ContentDto>(retrievedEntity);

            ResultDto.Data       = resultEntity;
            ResultDto.StatusCode = (int)StatusCodes.Created;
            return(ResultDto);
        }
        /// <summary>
        /// Upserts the content async.
        /// </summary>
        /// <returns>The content response async.</returns>
        /// <param name="content">Content.</param>
        /// <param name="direction">Content direction [Left/Right]</param>
        public async Task <ContentResponse> UpsertContentAsync(Content content, ContentDirection direction)
        {
            if (content == null)
            {
                return(new ContentResponse("'Content' should not be null."));
            }

            var result = await GetContentAsync(content.Id).ConfigureAwait(false);

            try
            {
                if (result.Success)
                {
                    if (direction == ContentDirection.Left)
                    {
                        result.Content.LeftContentData = content.LeftContentData;
                    }
                    else
                    {
                        result.Content.RightContentData = content.RightContentData;
                    }

                    _contentRepository.Update(result.Content);
                }
                else
                {
                    await _contentRepository.AddAsync(content);
                }

                await _unitOfWork.CommitAsync();

                return(new ContentResponse(content));
            }
            catch (Exception ex)
            {
                return(new ContentResponse($"An error occurred when saving the content: {ex.Message}"));
            }
        }