public async Task <ResponseModel <bool> > Handle(CreateCategoryCommand request, CancellationToken cancellationToken) { var response = new ResponseModel <bool>() { Status = HttpStatusCode.InternalServerError, IsSuccessful = false, Result = false, Errors = default }; try { var parentCategory = default(Data.Category); if (!string.IsNullOrEmpty(request.ParentId)) { parentCategory = await _categoryRepository.GetById(request.ParentId); if (parentCategory is null) { _logger.LogWarning($"{request.ParentId} is deleted"); return(_customExceptionBuilder.BuildEntityNotFoundException(response, request.Id, ErrorTypes.EntityNotFound)); } } var category = new Data.Category() { ParentId = request.ParentId, Description = request.Description, Name = request.Name, Parent = parentCategory, Id = request.Id }; await _categoryRepository.CreateAsync(category); response.Status = HttpStatusCode.Created; response.IsSuccessful = true; response.Result = true; return(response); } catch (Exception e) { _logger.LogCritical(e, e.StackTrace, "Method : CreateCategoryCommandHandler - Handle"); var identicalPrimaryKeyExceptionResponse = _customExceptionBuilder.BuildIdenticalPrimaryKeyException(e, response, request.Id); if (identicalPrimaryKeyExceptionResponse != null) { return(identicalPrimaryKeyExceptionResponse); } } response.Errors = new List <ErrorResponse>() { new ErrorResponse() { Reason = 500, Message = "An unexpected error occured" } }; return(response); }
public async Task <ResponseModel <bool> > Handle(CreatePostCommand request, CancellationToken cancellationToken) { var response = new ResponseModel <bool>() { Status = HttpStatusCode.InternalServerError, IsSuccessful = false, Result = false, Errors = default }; try { var category = await _categoryRepository.GetById(request.CategoryId); if (category is null) { _logger.LogWarning($"{request.CategoryId} is not found"); return(_customExceptionBuilder.BuildEntityNotFoundException(response, request.CategoryId, ErrorTypes.EntityNotFound)); } var post = new Data.Post() { Category = category, PostStatus = request.PostStatus, Content = _htmlSanitizerHelper.SanitizeInput(request.Content), Title = _htmlSanitizerHelper.SanitizeInput(request.Title), Id = request.Id }; await _postRepository.CreateAsync(post); response.Status = HttpStatusCode.Created; response.IsSuccessful = true; response.Result = true; return(response); } catch (Exception e) { if (e is ArgumentException) { _logger.LogCritical(e, e.StackTrace, "Method : CreatePostCommandHandler - Handle - ArgumentException"); return(_customExceptionBuilder.BuildMaliciousInputFoundException(response, e.Message, ErrorTypes.MaliciousInput)); } _logger.LogCritical(e, e.StackTrace, "Method : CreatePostCommandHandler - Handle"); var identicalPrimaryKeyExceptionResponse = _customExceptionBuilder.BuildIdenticalPrimaryKeyException(e, response, request.Id); if (identicalPrimaryKeyExceptionResponse != null) { return(identicalPrimaryKeyExceptionResponse); } } response.Errors = new List <ErrorResponse>() { new ErrorResponse() { Reason = 500, Message = "An unexpected error occured" } }; return(response); }