public async Task <List <DictionaryItemView> > GetItemViewsByUserId(string userId) { var words = _dictionaryAcessService .GetAll() .Where(it => it.UserId == userId); var items = new List <DictionaryItemView>(); foreach (var word in words) { var item = new DictionaryItemView(); item.Id = word.Id; item.Term = word.Text; item.Translation = word.Translations.FirstOrDefault()?.Text ?? "no translation"; var encodedImage = string.Empty; if (!string.IsNullOrEmpty(word.ImageId)) { var sourceImage = await _imageManager.Get(Folder.Dictionary, word.ImageId); encodedImage = FileConverter.ToBase64String(sourceImage); } item.Image = encodedImage; items.Add(item); } return(items); }
public static DictionaryItemView ToDictionaryItemView(this Word word) { var item = new DictionaryItemView { Id = word.Id, Term = word.Text, Translation = word.Translations.FirstOrDefault()?.Text ?? "no translation", ImageId = word.ImageId }; return(item); }
public async Task <IActionResult> Post([FromBody] DictionaryItemView item) { if (ModelState.IsValid) { var userId = _userManager.GetUserId(User); await _dictionaryService.SaveItemViewByUserId(item, userId); return(Ok()); } return(BadRequest(item)); }
public async Task SaveItemViewByUserId(DictionaryItemView item, string userId) { var word = item.ToWordWithUserId(userId); if (!string.IsNullOrEmpty(item.Image)) { var imageId = await _imageManager.Save(FileConverter.FromBase64String(item.Image), Folder.Dictionary); word.ImageId = imageId; } _dictionaryAcessService.Save(word); }
public static Word ToWordWithUserId(this DictionaryItemView item, string userId) { var word = new Word(); word.Text = item.Term; word.UserId = userId; var translation = new Translation { Text = item.Translation, Word = word }; word.Translations.Add(translation); return(word); }