public async Task <IActionResult> ImportFromJson([FromForm] IFormFile file)
        {
            try
            {
                if (file.Length > 0)
                {
                    if (file.ContentType.ToLower().Equals("application/json"))
                    {
                        var words  = new List <Word>();
                        var result = await file.DeserializeJson();

                        foreach (var jToken in result)
                        {
                            var word = new Word()
                            {
                                WordCategory = (string)jToken["wordCategory"],
                                WordImg      = (string)jToken["wordImg"],
                                PublicId     = (string)jToken["publicId"],
                                Eng          = (string)jToken["eng"],
                                Vie          = (string)jToken["vie"],
                                Spelling     = (string)jToken["spelling"],
                                WordVoice    = (string)jToken["wordVoice"]
                            };
                            var wordFromDb = await _repo.GetOneWithCondition <Word>(w => w.Eng.ToLower().Trim().Equals(word.Eng.ToLower().Trim()));

                            if (wordFromDb == null)
                            {
                                words.Add(word);
                                _repo.Create(word);
                            }
                        }
                        if (await _repo.SaveAll())
                        {
                            return(Ok(words));
                        }
                        else
                        {
                            return(NoContent());
                        }
                    }
                    return(BadRequest(new
                    {
                        error = "File not supported"
                    }));
                }
                return(NoContent());
            }
            catch (System.Exception e)
            {
                throw e;
            }
        }
Exemple #2
0
        public async Task <IActionResult> ImportJson([FromForm] IFormFile file)
        {
            try
            {
                if (file.Length > 0)
                {
                    var examples = new List <Example>();
                    if (file.ContentType.ToLower().Equals("application/json"))
                    {
                        var result = await file.DeserializeJson();

                        foreach (var item in result)
                        {
                            var example = new Example()
                            {
                                Eng      = (string)item["eng"],
                                Vie      = (string)item["vie"],
                                Inserted = DateTime.Now
                            };
                            if (!await _repo.Exists(example))
                            {
                                examples.Add(example);
                                _repo.Create(example);
                            }
                        }
                        if (await _repo.SaveAll())
                        {
                            return(Ok(examples));
                        }
                        return(NoContent());
                    }
                }
                return(NoContent());
            }
            catch (System.Exception e)
            {
                throw e;
            }
        }
        public async Task <IActionResult> ImportJson([FromForm] IFormFile file)
        {
            if (file.Length > 0)
            {
                if (file.ContentType.Equals("application/json"))
                {
                    var filters = new List <StringFilter>();
                    var result  = await file.DeserializeJson();

                    foreach (var item in result)
                    {
                        var filter = new StringFilter()
                        {
                            Word     = (string)item["word"],
                            Inserted = DateTime.Now
                        };
                        var filterFromDb = await _repo.GetOneWithCondition <StringFilter>(f => f.Word.ToLower().Trim().Equals(filter.Word.ToLower().Trim()));

                        if (filterFromDb == null)
                        {
                            filters.Add(filter);
                            _repo.Create(filter);
                        }
                        if (await _repo.SaveAll())
                        {
                            return(Ok(filters));
                        }
                        return(NoContent());
                    }
                }
                return(BadRequest(new
                {
                    error = "File not supported"
                }));
            }
            return(NoContent());
        }