public async Task <ActionResult <PersonalDictionary> > Post(int id) { //Получение базового словаря Dictionary dictionary = await db.Dictionaries.Where(d => d.Id == id) .Include(d => d.Words) .FirstOrDefaultAsync(d => d.Id == id); if (dictionary == null) { return(NotFound()); } if (!dictionary.IsPublic) { if (dictionary.AuthorId != Account.Id) { return(StatusCode(403)); } } PersonalDictionary PersonalDictionary = await db.PersonalDictionaries .Where(pd => pd.Dictionary.Id == dictionary.Id && pd.UserId == Account.Id) .Include(pd => pd.Dictionary) .Include(pd => pd.PersonalWords) .ThenInclude(d => d.Word) .FirstOrDefaultAsync(); //Проверка на наличие такого персонального словаря if (PersonalDictionary != null) { PersonalDictionary.Dictionary.Words = null; return(Ok(PersonalDictionary)); } PersonalDictionary = new PersonalDictionary(dictionary); PersonalDictionary.UserId = Account.Id; foreach (PersonalWord pw in PersonalDictionary.PersonalWords) { pw.UserId = Account.Id; } db.PersonalDictionaries.Add(PersonalDictionary); await db.SaveChangesAsync(); PersonalDictionary.Dictionary.Words = null; return(Ok(PersonalDictionary)); }
public async Task <ActionResult <PersonalDictionary> > Delete(int id) { PersonalDictionary dictionary = db.PersonalDictionaries .Where(x => x.Id == id) .Include(x => x.PersonalWords) .FirstOrDefault(); if (dictionary == null) { return(NotFound()); } if (dictionary.UserId != Account.Id) { return(StatusCode(403)); } db.PersonalDictionaries.Remove(dictionary); await db.SaveChangesAsync(); return(Ok()); }
public async Task <ActionResult <PersonalDictionary> > Put(PersonalDictionary dictionary) { if (dictionary == null) { return(BadRequest()); } if (dictionary.UserId != Account.Id) { return(StatusCode(403)); } if (!db.PersonalDictionaries.Any(x => x.Id == dictionary.Id)) { return(NotFound()); } db.Update(dictionary); await db.SaveChangesAsync(); return(Ok()); }
public async Task <ActionResult <PersonalDictionary> > Get(int id) { PersonalDictionary PersonalDictionary = await db.PersonalDictionaries .Where(x => x.Id == id) .Include(pd => pd.Dictionary) .Include(pd => pd.PersonalWords) .ThenInclude(pw => pw.Word) .FirstOrDefaultAsync(); if (PersonalDictionary == null) { return(NotFound()); } if (PersonalDictionary.UserId != Account.Id) { return(StatusCode(403)); } PersonalDictionary.Dictionary.Words = null; return(Ok(PersonalDictionary)); }