Ejemplo n.º 1
0
        private static void UpdateKnowledgeBase(KnowledgeBaseCreateRequest request, KnowledgeBase knowledgeBase)
        {
            knowledgeBase.CategoryId = request.CategoryId;

            knowledgeBase.Title = request.Title;

            if (string.IsNullOrEmpty(request.SeoAlias))
            {
                knowledgeBase.SeoAlias = TextHelper.ToUnsignString(request.Title);
            }
            else
            {
                knowledgeBase.SeoAlias = request.SeoAlias;
            }

            knowledgeBase.Description = request.Description;

            knowledgeBase.Environment = request.Environment;

            knowledgeBase.Problem = request.Problem;

            knowledgeBase.StepToReproduce = request.StepToReproduce;

            knowledgeBase.ErrorMessage = request.ErrorMessage;

            knowledgeBase.Workaround = request.Workaround;

            knowledgeBase.Note = request.Note;

            if (request.Labels != null)
            {
                knowledgeBase.Labels = string.Join(',', request.Labels);
            }
        }
Ejemplo n.º 2
0
        private static KnowledgeBase CreateKnowledgeBaseEntity(KnowledgeBaseCreateRequest request)
        {
            var entity = new KnowledgeBase()
            {
                CategoryId = request.CategoryId,

                Title = request.Title,

                SeoAlias = request.SeoAlias,

                Description = request.Description,

                Environment = request.Environment,

                Problem = request.Problem,

                StepToReproduce = request.StepToReproduce,

                ErrorMessage = request.ErrorMessage,

                Workaround = request.Workaround,

                Note = request.Note
            };

            if (request.Labels?.Length > 0)
            {
                entity.Labels = string.Join(',', request.Labels);
            }
            return(entity);
        }
Ejemplo n.º 3
0
        private async Task ProcessLabel(KnowledgeBaseCreateRequest request, KnowledgeBase knowledgeBase)
        {
            foreach (var labelText in request.Labels)
            {
                if (labelText == null)
                {
                    continue;
                }
                var labelId       = TextHelper.ToUnsignString(labelText.ToString());
                var existingLabel = await _context.Labels.FindAsync(labelId);

                if (existingLabel == null)
                {
                    var labelEntity = new Label()
                    {
                        Id   = labelId,
                        Name = labelText.ToString()
                    };
                    _context.Labels.Add(labelEntity);
                }
                if (await _context.LabelInKnowledgeBases.FindAsync(labelId, knowledgeBase.Id) == null)
                {
                    _context.LabelInKnowledgeBases.Add(new LabelInKnowledgeBase()
                    {
                        KnowledgeBaseId = knowledgeBase.Id,
                        LabelId         = labelId
                    });
                }
            }
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> PutKnowledgeBase(int id, [FromForm] KnowledgeBaseCreateRequest request)
        {
            var knowledgeBase = await _context.KnowledgeBases.FindAsync(id);

            if (knowledgeBase == null)
            {
                return(NotFound(new ApiNotFoundResponse($"Cannot found knowledge base with id {id}")));
            }
            UpdateKnowledgeBase(request, knowledgeBase);

            //Process attachment
            if (request.Attachments != null && request.Attachments.Count > 0)
            {
                foreach (var attachment in request.Attachments)
                {
                    var attachmentEntity = await SaveFile(knowledgeBase.Id, attachment);

                    _context.Attachments.Add(attachmentEntity);
                }
            }
            _context.KnowledgeBases.Update(knowledgeBase);

            if (request.Labels?.Length > 0)
            {
                await ProcessLabel(request, knowledgeBase);
            }
            var result = await _context.SaveChangesAsync();

            if (result > 0)
            {
                return(NoContent());
            }
            return(BadRequest(new ApiBadRequestResponse($"Update knowledge base failed")));
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> PostKnowledgeBase([FromForm] KnowledgeBaseCreateRequest request)
        {
            _logger.LogInformation("Begin PostKnowledgeBase API");
            KnowledgeBase knowledgeBase = CreateKnowledgeBaseEntity(request);

            knowledgeBase.OwnerUserId = User.GetUserId();
            if (string.IsNullOrEmpty(knowledgeBase.SeoAlias))
            {
                knowledgeBase.SeoAlias = TextHelper.ToUnsignString(knowledgeBase.Title);
            }
            knowledgeBase.Id = await _sequenceService.GetKnowledgeBaseNewId();

            //Process attachment
            if (request.Attachments != null && request.Attachments.Count > 0)
            {
                foreach (var attachment in request.Attachments)
                {
                    var attachmentEntity = await SaveFile(knowledgeBase.Id, attachment);

                    _context.Attachments.Add(attachmentEntity);
                }
            }
            _context.KnowledgeBases.Add(knowledgeBase);

            //Process label
            if (request.Labels?.Length > 0)
            {
                await ProcessLabel(request, knowledgeBase);
            }

            var result = await _context.SaveChangesAsync();

            if (result > 0)
            {
                await _cacheService.RemoveAsync(CacheConstants.LatestKnowledgeBases);

                await _cacheService.RemoveAsync(CacheConstants.PopularKnowledgeBases);

                _logger.LogInformation("End PostKnowledgeBase API - Success");

                await _oneSignalService.SendAsync(request.Title, request.Description,
                                                  string.Format(CommonConstants.KnowledgeBaseUrl, knowledgeBase.SeoAlias, knowledgeBase.Id));

                return(CreatedAtAction(nameof(GetById), new
                {
                    id = knowledgeBase.Id
                }));
            }
            else
            {
                _logger.LogInformation("End PostKnowledgeBase API - Failed");

                return(BadRequest(new ApiBadRequestResponse("Create knowledge failed")));
            }
        }
        public async Task <IActionResult> PostKnowledgeBase([FromForm] KnowledgeBaseCreateRequest request)
        {
            //// CREATE NEW INSTANCE OF KNOWLEDGE BASE WITH INFOS ARE INPUT DATA
            KnowledgeBase knowledgeBase = CreateKnowledgeBaseEntity(request);

            knowledgeBase.Id = await _sequenceService.GetKnowledgeBaseNewId();

            //// GET CURRENT ID (IS OWNER USER)
            knowledgeBase.OwnerUserId = User.GetUserId();
            //// CONVERT SIGN STRING TO UNSIGN STRING
            if (string.IsNullOrEmpty(knowledgeBase.SeoAlias))
            {
                knowledgeBase.SeoAlias = TextHelper.ToUnsignString(knowledgeBase.Title);
            }

            //// PROCESS ATTACHMENT
            if (request.Attachments != null && request.Attachments.Count > 0)
            {
                foreach (var attachment in request.Attachments)
                {
                    var attachmentEntity = await SaveFile(knowledgeBase.Id, attachment);

                    _context.Attachments.Add(attachmentEntity);
                }
            }

            _context.KnowledgeBases.Add(knowledgeBase);

            //// PROCESS LABEL
            if (request.Labels?.Length > 0)
            {
                await ProcessLabel(request, knowledgeBase);
            }

            //// INSERT NEW KNOWLEDGE BASE INTO DATABASE AND SAVE CHANGE
            _context.KnowledgeBases.Add(knowledgeBase);
            var result = await _context.SaveChangesAsync();

            //// IF RESULT AFTER INSERT IS GREATER THAN 0 (TRUE), RETURN STATUS 201, ELSE RETURN STATUS 400
            if (result > 0)
            {
                await _cacheService.RemoveAsync(CacheConstants.LatestKnowledgeBases);

                await _cacheService.RemoveAsync(CacheConstants.PopularKnowledgeBases);

                return(CreatedAtAction(nameof(GetById), new { id = knowledgeBase.Id }));
            }
            else
            {
                return(BadRequest(new ApiBadRequestResponse("Create knowledge base failed")));
            }
        }
        public async Task <IActionResult> CreateNewKnowledgeBase([FromForm] KnowledgeBaseCreateRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var result = await _knowledgeBaseApiClient.PostKnowlegdeBase(request);

            if (result)
            {
                return(Ok());
            }
            return(BadRequest());
        }
Ejemplo n.º 8
0
        public async Task <bool> PostKnowlegdeBase(KnowledgeBaseCreateRequest request)
        {
            var client = _httpClientFactory.CreateClient("BackendApi");

            client.BaseAddress       = new Uri(_configuration["BackendApiUrl"]);
            using var requestContent = new MultipartFormDataContent();

            if (request.Attachments?.Count > 0)
            {
                foreach (var item in request.Attachments)
                {
                    byte[] data;
                    using (var br = new BinaryReader(item.OpenReadStream()))
                    {
                        data = br.ReadBytes((int)item.OpenReadStream().Length);
                    }
                    ByteArrayContent bytes = new ByteArrayContent(data);
                    requestContent.Add(bytes, "attachments", item.FileName);
                }
            }
            requestContent.Add(new StringContent(request.CategoryId.ToString()), "categoryId");
            requestContent.Add(new StringContent(request.Title.ToString()), "title");
            requestContent.Add(new StringContent(request.Problem.ToString()), "problem");
            requestContent.Add(new StringContent(request.Note.ToString()), "note");
            requestContent.Add(new StringContent(request.Description.ToString()), "description");
            requestContent.Add(new StringContent(request.Environment.ToString()), "environment");
            requestContent.Add(new StringContent(request.StepToReproduce.ToString()), "stepToReproduce");
            requestContent.Add(new StringContent(request.ErrorMessage.ToString()), "errorMessage");
            requestContent.Add(new StringContent(request.Workaround.ToString()), "workaround");
            if (request.Labels?.Length > 0)
            {
                foreach (var label in request.Labels)
                {
                    requestContent.Add(new StringContent(label), "labels");
                }
            }

            var token = await _httpContextAccessor.HttpContext.GetTokenAsync("access_token");

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

            var response = await client.PostAsync($"/api/knowledgeBases/", requestContent);

            return(response.IsSuccessStatusCode);
        }
        public async Task <IActionResult> PutKnowledgeBase(int id, [FromForm] KnowledgeBaseCreateRequest request)
        {
            //// GET KNOWLEDGE BASE WITH ID (KEY)
            var knowledgeBase = await _context.KnowledgeBases.FindAsync(id);

            //// IF KEY IS NOT EXIST (RESULT IS NULL), RETURN STATUS 404
            if (knowledgeBase == null)
            {
                return(NotFound(new ApiNotFoundResponse($"Cannot found knowledge base with id {id}")));
            }

            //// GIVE INPUT DATA FOR EACH FIELD OF OBJECT WHICH NEED UPDATE INFOMATIONS
            UpdateKnowledgeBase(request, knowledgeBase);

            //// PROCESS ATTACHMENT
            if (request.Attachments != null && request.Attachments.Count > 0)
            {
                foreach (var attachment in request.Attachments)
                {
                    var attachmentEntity = await SaveFile(knowledgeBase.Id, attachment);

                    _context.Attachments.Add(attachmentEntity);
                }
            }
            //// PROCESS LABEL
            if (request.Labels?.Length > 0)
            {
                await ProcessLabel(request, knowledgeBase);
            }

            //// UPDATE KNOWLEDGE BASE AND SAVE CHANGE
            _context.KnowledgeBases.Update(knowledgeBase);
            var result = await _context.SaveChangesAsync();

            //// IF RESULT AFTER UPDATE IS GREATER THAN 0 (TRUE), RETURN STATUS 204, ELSE RETURN STATUS 400
            if (result > 0)
            {
                await _cacheService.RemoveAsync(CacheConstants.LatestKnowledgeBases);

                await _cacheService.RemoveAsync(CacheConstants.PopularKnowledgeBases);

                return(NoContent());
            }
            return(BadRequest(new ApiBadRequestResponse($"Update knowledge base failed")));
        }
Ejemplo n.º 10
0
        public async Task <IActionResult> CreateNewKnowledgeBase([FromForm] KnowledgeBaseCreateRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (!Captcha.ValidateCaptchaCode(request.CaptchaCode, HttpContext))
            {
                ModelState.AddModelError("", "Mã xác nhận không đúng");
                return(BadRequest(ModelState));
            }

            var result = await _knowledgeBaseApiClient.PostKnowlegdeBase(request);

            if (result)
            {
                return(Ok());
            }
            return(BadRequest());
        }
Ejemplo n.º 11
0
 public KnowledgeBaseCreateRequestValidatorTest()
 {
     request = new KnowledgeBaseCreateRequest()
     {
         CaptchaCode     = "abc",
         Labels          = new string[] { "test" },
         Environment     = "test",
         SeoAlias        = "test",
         Attachments     = null,
         CategoryId      = 1,
         Description     = "test",
         ErrorMessage    = "test",
         Note            = "test",
         Problem         = "test",
         StepToReproduce = "test",
         Title           = "test",
         Workaround      = "test"
     };
     validator = new KnowledgeBaseCreateRequestValidator();
 }
Ejemplo n.º 12
0
        public async Task <IActionResult> PutKnowledgeBase(int id, [FromBody] KnowledgeBaseCreateRequest request)
        {
            var knowledgeBase = await _context.KnowledgeBases.FindAsync(id);

            if (knowledgeBase == null)
            {
                return(NotFound());
            }

            knowledgeBase.CategoryId = request.CategoryId;

            knowledgeBase.Title = request.Title;

            knowledgeBase.SeoAlias = request.SeoAlias;

            knowledgeBase.Description = request.Description;

            knowledgeBase.Environment = request.Environment;

            knowledgeBase.Problem = request.Problem;

            knowledgeBase.StepToReproduce = request.StepToReproduce;

            knowledgeBase.ErrorMessage = request.ErrorMessage;

            knowledgeBase.Workaround = request.Workaround;

            knowledgeBase.Note = request.Note;

            knowledgeBase.Labels = request.Labels;

            _context.KnowledgeBases.Update(knowledgeBase);
            var result = await _context.SaveChangesAsync();

            if (result > 0)
            {
                return(NoContent());
            }
            return(BadRequest());
        }
Ejemplo n.º 13
0
        public async Task <IActionResult> PostKnowledgeBase([FromBody] KnowledgeBaseCreateRequest request)
        {
            var knowledgeBase = new KnowledgeBase()
            {
                CategoryId = request.CategoryId,

                Title = request.Title,

                SeoAlias = request.SeoAlias,

                Description = request.Description,

                Environment = request.Environment,

                Problem = request.Problem,

                StepToReproduce = request.StepToReproduce,

                ErrorMessage = request.ErrorMessage,

                Workaround = request.Workaround,

                Note = request.Note,

                Labels = request.Labels,
            };

            _context.KnowledgeBases.Add(knowledgeBase);
            var result = await _context.SaveChangesAsync();

            if (result > 0)
            {
                return(CreatedAtAction(nameof(GetById), new { id = knowledgeBase.Id }, request));
            }
            else
            {
                return(BadRequest());
            }
        }
        /// <summary>
        /// ADD LABEL FOR KNOWLEDGE BASE.
        /// </summary>
        /// <param name="request">INPUT DATA OF KNOWLEDGE BASE.</param>
        /// <param name="knowledgeBase"></param>
        /// <returns></returns>
        private async Task ProcessLabel(KnowledgeBaseCreateRequest request, KnowledgeBase knowledgeBase)
        {
            //// SPLIT STRING LABEL OF KNOWLEDGE BASE TO A ARRAY
            foreach (var labelText in request.Labels)
            {
                //// IF LABEL NULL IGNORE THIS LOOP
                if (labelText == null)
                {
                    continue;
                }

                //// CONVERT SEALED CHARACTERS TO UNSIGN STRING, AND IT IS ID OF LABEL
                var labelId = TextHelper.ToUnsignString(labelText.ToString());

                //// GET LABEL WITH ID, IF KEY NOT EXIST, CREATE NEW LABEL
                var existingLabel = await _context.Labels.FindAsync(labelId);

                if (existingLabel == null)
                {
                    var labelEntity = new Label()
                    {
                        Id   = labelId,
                        Name = labelText.ToString()
                    };
                    _context.Labels.Add(labelEntity);
                }

                //// ADD NEW LABEL FOR KNOWLEDGE BASE
                if (await _context.LabelInKnowledgeBases.FindAsync(labelId, knowledgeBase.Id) == null)
                {
                    _context.LabelInKnowledgeBases.Add(new LabelInKnowledgeBase()
                    {
                        KnowledgeBaseId = knowledgeBase.Id,
                        LabelId         = labelId
                    });
                }
            }
        }
Ejemplo n.º 15
0
        private static void UpdateKnowledgeBase(KnowledgeBaseCreateRequest request, KnowledgeBase knowledgeBase)
        {
            knowledgeBase.CategoryId = request.CategoryId;

            knowledgeBase.Title = request.Title;

            knowledgeBase.SeoAlias = request.SeoAlias;

            knowledgeBase.Description = request.Description;

            knowledgeBase.Environment = request.Environment;

            knowledgeBase.Problem = request.Problem;

            knowledgeBase.StepToReproduce = request.StepToReproduce;

            knowledgeBase.ErrorMessage = request.ErrorMessage;

            knowledgeBase.Workaround = request.Workaround;

            knowledgeBase.Note = request.Note;

            knowledgeBase.Labels = string.Join(',', request.Labels);
        }