public async Task <DataResult <CategoryModel> > AddCategoryAsync(CategoryAddDto category) { using (var context = new LangiumDbContext()) { try { var newCategory = new CategoryModel() { Name = category.Name, Description = category.Description, Words = new List <WordModel>(), ProfileId = category.ProfileId }; var categoryWithSameName = await context.Categories.FirstOrDefaultAsync(c => c.Name == category.Name && c.ProfileId == category.ProfileId); if (categoryWithSameName == null) { var added = await context.Categories.AddAsync(newCategory); await context.SaveChangesAsync(); return(new DataResult <CategoryModel>(added.Entity)); } else { return(new DataResult <CategoryModel>(null, "CATEGORY_WITH_SAME_NAME_AlREADY_EXISTS")); } } catch (Exception ex) { return(new DataResult <CategoryModel>(ex, ex.InnerException.Message)); } } }
public async Task <DataResult <bool> > RemoveCategoryAsync(int id) { using (var context = new LangiumDbContext()) { try { var entity = await context.Categories.FindAsync(id); if (entity == null) { return(new DataResult <bool>(null, "CATEGORY_NOT_EXISTS")); } context.Remove(entity); await context.SaveChangesAsync(); return(new DataResult <bool>(true)); } catch (Exception ex) { return(new DataResult <bool>(ex, ex.Message)); } } }
public async Task <DataResult <WordModel> > AddWordAsync(WordAddDto word) { using (var context = new LangiumDbContext()) { try { var newWord = new WordModel() { Lexeme = new LexemeModel() { Lexeme = word.Lexeme }, Transcription = new TranscriptionModel() { Transcription = word.Transcription }, Translation = new TranslationModel() { Translation = word.Translation }, CategoryId = word.CategoryId }; CategoryDao categoryDao = new CategoryDao(); var sameWords = categoryDao.GetCategoryByIdAsync(word.CategoryId).Result.Data.Words.Where(w => w.Lexeme.Lexeme == word.Lexeme); if (sameWords.Count() == 0) { var added = await context.Words.AddAsync(newWord); await context.SaveChangesAsync(); return(new DataResult <WordModel>(added.Entity)); } else { return(new DataResult <WordModel>(null, "WORD_WITH_SAME_LEXEM_AlREADY_EXISTS")); } } catch (Exception ex) { return(new DataResult <WordModel>(ex, ex.InnerException.Message)); } } }
public async Task <DataResult <UserModel> > AddUserAsync(UserAuthDto user) { using (var context = new LangiumDbContext()) { try { var newUser = new UserModel() { Email = user.Email, Password = user.Password, ActivationCode = ActivationHelper.GetActivationCode(), Profile = new ProfileModel { Stats = new StatsModel(), Categories = new List <CategoryModel>() { new CategoryModel() { Name = "Unsorted Words" } } } }; var added = await context.Users.AddAsync(newUser); await context.SaveChangesAsync(); return(new DataResult <UserModel> (added.Entity)); } catch (Exception ex) { if (ex.InnerException.Message.Contains("23505")) { return(new DataResult <UserModel>(ex, "USER_AlREADY_EXISTS")); } else { return(new DataResult <UserModel>(ex, ex.InnerException.Message)); } } } }
public async Task <DataResult <UserModel> > UpdateUserAsync(UserModel user) { using (var context = new LangiumDbContext()) { try { context.Entry(user).State = EntityState.Modified; await context.SaveChangesAsync(); var updatedUser = context.Users.FirstOrDefault(u => u.Id == user.Id); return(new DataResult <UserModel> (updatedUser)); } catch (Exception ex) { return(new DataResult <UserModel>(ex, ex.InnerException.Message)); } } }
public async Task <DataResult <CategoryModel> > UpdateCategoryAsync(CategoryModel category) { using (var context = new LangiumDbContext()) { try { context.Entry(category).State = EntityState.Modified; await context.SaveChangesAsync(); var updatedCategory = context.Categories.FirstOrDefault(c => c.Id == category.Id); return(new DataResult <CategoryModel>(updatedCategory)); } catch (Exception ex) { return(new DataResult <CategoryModel>(ex, ex.InnerException.Message)); } } }
public async Task <DataResult <WordModel> > UpdateWordAsync(WordModel word) { using (var context = new LangiumDbContext()) { try { context.Entry(word).State = EntityState.Modified; context.Entry(word.Lexeme).State = EntityState.Modified; context.Entry(word.Transcription).State = EntityState.Modified; context.Entry(word.Translation).State = EntityState.Modified; await context.SaveChangesAsync(); var updatedWord = context.Words.FirstOrDefault(w => w.Id == word.Id); return(new DataResult <WordModel>(updatedWord)); } catch (Exception ex) { return(new DataResult <WordModel>(ex, ex.InnerException.Message)); } } }