public async Task <string> GetCategoryName(int categoryId) { using (var db = new ThingsToDo.DAL.ThingsToDoAppContext()) { var category = await db.Categories.FirstOrDefaultAsync(x => x.Id == categoryId); return(category != null ? category.Title : ""); } }
public async Task <ServiceActionResult <List <CategoryModel> > > GetAllCategoriesAsync(int userId) { using (var db = new ThingsToDo.DAL.ThingsToDoAppContext()) { var categories = await db.Categories.Where(x => x.UserId == userId).Select(x => new CategoryModel { Id = x.Id, Title = x.Title }).ToListAsync(); if (categories != null) { return(new ServiceActionResult <List <CategoryModel> >(categories, ServiceActionStatus.Ok, Resources.ThingsToDo.lblFound)); } else { return(new ServiceActionResult <List <CategoryModel> >(categories, ServiceActionStatus.Error, Resources.ThingsToDo.Global_InternalServerError)); } } }
public async Task <ServiceActionResult <CategoryModel> > CreateCategory(CategoryModel model) { using (var db = new ThingsToDo.DAL.ThingsToDoAppContext()) { db.Categories.Add(new Categories { Title = model.Title, UserId = model.UserId }); var result = await db.SaveChangesAsync(); if (result > 0) { return(new ServiceActionResult <CategoryModel>(model, ServiceActionStatus.Created)); } else { return(new ServiceActionResult <CategoryModel>(model, ServiceActionStatus.Error, Resources.ThingsToDo.lblTaskCreateError)); } } }