示例#1
0
        public async Task <IActionResult> Add([FromBody] DictionaryRequest request)
        {
            string accessToken = await HttpContext.GetToken();

            var session = await sessionService.GetSession(accessToken);

            if (session == null)
            {
                return(Unauthorized(new { message = "Session expired. Please login again." }));
            }

            if (session.UserId != Constants.AdminUser)
            {
                return(BadRequest(new { message = "Not authorized" }));
            }

            try
            {
                var lexicon = new Lexicon
                {
                    CreationDate = DateTime.Now,
                    Language     = request.Language,
                    Description  = request.Description,
                };

                var words = request.Words.Select(w => new Word
                {
                    Language = lexicon.Language,
                    Name     = w
                });

                await log.InfoAsync($"Creating new Lexicon: [{lexicon.Language}]", context : session.UserId);

                var isOK = await wordRepository.AddDictionary(lexicon, words);

                if (isOK)
                {
                    await log.InfoAsync($"New Lexicon Created: [{lexicon.Language}]", context : session.UserId);

                    return(Ok(isOK));
                }
                throw new ApplicationException("Lexicon creation failed");
            }
            catch (Exception ex)
            {
                await log.ErrorAsync("Error in wordRepository.AddDictionary()", ex);

                return(BadRequest(new { title = ex.GetType().ToString(), details = ex.StackTrace, message = ex.Message }));
            }
        }