diff --git a/MirysList/Controllers/ShopController.cs b/MirysList/Controllers/ShopController.cs index d34c96d..2dd2570 100644 --- a/MirysList/Controllers/ShopController.cs +++ b/MirysList/Controllers/ShopController.cs @@ -34,16 +34,19 @@ public IActionResult Catalog() [HttpGet] [Route("api/Shop/Catalog/{catalogId}")] public IActionResult CataLogItems(int catalogId) - { - IQueryable cItems = _dbContext.CataLogItems.Where(x => x.Catalog.Id == catalogId).Include(y => y.Category); - if (cItems != null) - { - return Ok(cItems); - } - else + { + + Catalog catalog = _dbContext.Catalogs.Where(x => x.Id == catalogId).Include(c => c.Items).FirstOrDefault(); + if (catalog != null) { - return NotFound("could not find any catalog items for catalogid " + catalogId); + ICollection cItems = catalog.Items; + if (cItems != null) + { + return Ok(cItems); + } } + + return NotFound("could not find any catalog items for catalogid " + catalogId); } // GET: api/Shop/List @@ -52,13 +55,13 @@ public IActionResult CataLogItems(int catalogId) [Route("api/Shop/ShoppingList/{familyId}")] public IActionResult ShoppingList(int familyId) { - Family familyObj = _dbContext.Families.Where(x => x.Id == familyId).Include(u => u.shoppingList).FirstOrDefault(); + Family familyObj = _dbContext.Families.Where(x => x.Id == familyId).Include(u => u.listItems).FirstOrDefault(); if (familyObj != null) { - ShoppingList listObj = familyObj.shoppingList; - if (listObj != null) + List listItems = familyObj.listItems; + if (listItems != null) { - return Ok(listObj); + return Ok(listItems); } } @@ -67,68 +70,78 @@ public IActionResult ShoppingList(int familyId) // GET: api/Shop/ListItems //param: listId - [HttpGet] + /*[HttpGet] [Route("api/Shop/ShoppingListItems/{shoppinglistId}")] public IActionResult ShoppingListItems(int shoppinglistId) { - IQueryable listItems = _dbContext.ShoppingListItems.Where(x => x.ShoppingList.Id == shoppinglistId).Include(u => u.CatalogItem); - if (listItems != null) - { - return Ok(listItems); + ShoppingList list = _dbContext.ShoppingLists.Where(x => x.Id == shoppinglistId).FirstOrDefault(); + if (list != null) + { + ICollection listItems = list.listItems; + if (listItems != null) + { + return Ok(listItems); + } } return NotFound("could not find a list for this id " + shoppinglistId); - } + }*/ // POST: api/Shop/CreateList [HttpPost] - [Route("api/Shop/CreateList/familyId")] - public IActionResult CreateList([FromBody]ShoppingList listObj, int familyId) - { + [Route("api/Shop/CreateList/{familyId}")] + public IActionResult CreateList([FromBody]List listItems, int familyId) + { + Family familyObj = null; try { - Family familyObj = _dbContext.Families.Where(x => x.Id == familyId).FirstOrDefault(); + familyObj = _dbContext.Families.Where(x => x.Id == familyId).FirstOrDefault(); if (familyObj != null) - { - _dbContext.ShoppingLists.Add(listObj); - foreach (ShoppingListItem item in listObj.listItems) + { + foreach (ShoppingListItem item in listItems) { _dbContext.ShoppingListItems.Add(item); + familyObj.listItems.Add(item); + _dbContext.Families.Update(familyObj); } - familyObj.shoppingList = listObj; + _dbContext.SaveChanges(); + return Ok(familyObj); } } catch(Exception e) { - return NotFound("could not create a list : " + e.Message); + return NotFound("could not create a list : " + e); } - return Ok(listObj); + return NotFound("could not create a list"); } // POST: api/Shop/UpdateList [HttpPost] - [Route("api/Shop/UpdateList/{listId}")] - public IActionResult UpdateList([FromBody]ShoppingList updatedListObj, int listId) + [Route("api/Shop/UpdateList/{familyId}")] + public IActionResult UpdateList([FromBody]List updatedListItems, int familyId) { - ShoppingList listObj; + Family familyObj; try { - listObj = _dbContext.ShoppingLists.Where(x => x.Id == listId).FirstOrDefault(); - if(listObj != null && listObj.Id == updatedListObj.Id) - { - _dbContext.ShoppingLists.Update(updatedListObj); - _dbContext.SaveChanges(); - } + familyObj = _dbContext.Families.Where(x => x.Id == familyId).FirstOrDefault(); + if (familyObj != null) + { + foreach (ShoppingListItem item in updatedListItems) + { + _dbContext.ShoppingListItems.Update(item); + _dbContext.SaveChanges(); + } + } } catch(Exception e) { return NotFound("could not update a list : " + e.Message); } - return Ok(updatedListObj); + return Ok(updatedListItems); } } } diff --git a/MirysList/MirysList.csproj b/MirysList/MirysList.csproj index 111db2e..69503e6 100644 --- a/MirysList/MirysList.csproj +++ b/MirysList/MirysList.csproj @@ -7,6 +7,7 @@ + diff --git a/MirysList/Models/AppDbContext.cs b/MirysList/Models/AppDbContext.cs index 53624c5..9222ee7 100644 --- a/MirysList/Models/AppDbContext.cs +++ b/MirysList/Models/AppDbContext.cs @@ -20,7 +20,7 @@ public AppDbContext(DbContextOptions options) : base(options) public DbSet Catalogs { get; set; } public DbSet CataLogItems { get; set; } public DbSet Categories { get; set; } - public DbSet ShoppingLists { get; set; } + // public DbSet ShoppingLists { get; set; } public DbSet ShoppingListItems { get; set; } public DbSet Users { get; set; } public DbSet UserRoles { get; set; } diff --git a/MirysList/Models/CatalogItem.cs b/MirysList/Models/CatalogItem.cs index 02f14f3..e38169a 100644 --- a/MirysList/Models/CatalogItem.cs +++ b/MirysList/Models/CatalogItem.cs @@ -15,7 +15,7 @@ public class CatalogItem public string Title { get; set; } public string ImageUrl { get; set; } public Category Category { get; set; } - public Catalog Catalog { get; set; } + // public Catalog Catalog { get; set; } public string ItemNotes { get; set; } } } diff --git a/MirysList/Models/Family.cs b/MirysList/Models/Family.cs index 898feda..13b588c 100644 --- a/MirysList/Models/Family.cs +++ b/MirysList/Models/Family.cs @@ -46,6 +46,6 @@ public class Family public virtual ICollection FamilyMembers {get; set;} - public ShoppingList shoppingList { get; set; } + public List listItems { get; set; } } } diff --git a/MirysList/Models/ShoppingListItem.cs b/MirysList/Models/ShoppingListItem.cs index 2d60bb9..07d7994 100644 --- a/MirysList/Models/ShoppingListItem.cs +++ b/MirysList/Models/ShoppingListItem.cs @@ -14,8 +14,8 @@ public class ShoppingListItem public CatalogItem CatalogItem { get; set; } [Required] public int Quantity { get; set; } - public ShoppingList ShoppingList { get; set; } - [Required] - public User User { get; set; } + // public ShoppingList ShoppingList { get; set; } + //[Required] + public string ItemNotes { get; set; } } } diff --git a/MirysList/appsettings.json b/MirysList/appsettings.json index 994b859..e11715e 100644 --- a/MirysList/appsettings.json +++ b/MirysList/appsettings.json @@ -14,6 +14,6 @@ }, "ConnectionStrings": { "DbContextConnectionString": "Data Source=tcp:miryslistserver.database.windows.net,1433;Initial Catalog=MirysListDb;User ID=miryslistserver;Password=Hackathon123" - //"DbContextConnectionString": "Data Source=swapnag1;Initial Catalog=Miryslist1;Integrated Security=True" + // "DbContextConnectionString": "Data Source=swapnag1;Initial Catalog=Miryslist1;Integrated Security=True" } }