예제 #1
0
        public async Task <MaterialResponseDto> GetMaterial(long id)
        {
            var response = new MaterialResponseDto
            {
                // TODO: change to non-recursive Include()
                Material = await _materialsRepository.GetAll().Where(m => m.Id == id).Select(m => new Material
                {
                    Description = m.Description,
                    Id          = m.Id,
                    Image       = m.Image,
                    Words       = m.Words.Select(w => new Word {
                        Id = w.Id, Count = w.Count, TheWord = w.TheWord
                    }).ToList(),
                    Name  = m.Name,
                    Type  = m.Type,
                    Owner = m.Owner
                }).FirstOrDefaultAsync()
            };

            if (response.Material == null)
            {
                throw new UserFriendlyException("Material with key " + id + " does not exist!");
            }

            var account = await GetCurrentUserOrNullAsync();

            response.VocabWords = await _vocabularyService.GetSpecifiedVocabWordsAsync(response.Material.Words, account);

            return(response);
        }
예제 #2
0
        public async Task <string> FileAsync(IFormFile file)
        {
            if (file != null)
            {
                if (file.Length > 35000000)
                {
                    throw new UserFriendlyException("Subtitles file too big!");
                }
                else if (file.Length > 0)
                {
                    try
                    {
                        var stream  = file.OpenReadStream();
                        var words   = _parser.ParseUnigueWordsInFile(new Material(), new StreamReader(stream));
                        var account = await GetCurrentUserOrNullAsync();

                        var vocabWords = await _vocabularyService.GetSpecifiedVocabWordsAsync(words, account);

                        if (words.Count > 0)
                        {
                            var response = new ParseResponseDto {
                                Words = words, VocabWords = vocabWords
                            };

                            return(ApiJsonSerializer.Serialize(response));
                        }
                    }
                    catch (Exception ex)
                    {
                        Trace.TraceError(ex.ToString());
                        throw new UserFriendlyException("Server error. Please try again later.");
                    }
                }
            }
            else
            {
                throw new UserFriendlyException("Empty subtitles file!");
            }

            throw new UserFriendlyException("Server error. Please try again later.");
        }