From 13967b3ab69451d66bcc9c55506235bead857914 Mon Sep 17 00:00:00 2001 From: Swapna Guddanti Date: Tue, 24 Jul 2018 14:12:10 -0700 Subject: [PATCH] fixing api and models --- MirysList/Controllers/ShopController.cs | 38 +++++++++++++++---------- MirysList/Models/Family.cs | 2 ++ MirysList/Models/ShoppingList.cs | 2 +- MirysList/appsettings.json | 2 +- 4 files changed, 27 insertions(+), 17 deletions(-) diff --git a/MirysList/Controllers/ShopController.cs b/MirysList/Controllers/ShopController.cs index 18e3b1a..d34c96d 100644 --- a/MirysList/Controllers/ShopController.cs +++ b/MirysList/Controllers/ShopController.cs @@ -52,10 +52,14 @@ public IActionResult CataLogItems(int catalogId) [Route("api/Shop/ShoppingList/{familyId}")] public IActionResult ShoppingList(int familyId) { - ShoppingList listObj = _dbContext.ShoppingLists.Where(x => x.Id == familyId).Include(u => u.Family).Include(y => y.listItems).FirstOrDefault(); - if (listObj != null) + Family familyObj = _dbContext.Families.Where(x => x.Id == familyId).Include(u => u.shoppingList).FirstOrDefault(); + if (familyObj != null) { - return Ok(listObj); + ShoppingList listObj = familyObj.shoppingList; + if (listObj != null) + { + return Ok(listObj); + } } return NotFound("could not find a list for this family " + familyId); @@ -79,22 +83,26 @@ public IActionResult ShoppingListItems(int shoppinglistId) // POST: api/Shop/CreateList [HttpPost] - [Route("api/Shop/CreateList")] - public IActionResult CreateList([FromBody]ShoppingList listObj) + [Route("api/Shop/CreateList/familyId")] + public IActionResult CreateList([FromBody]ShoppingList listObj, int familyId) { try { - _dbContext.ShoppingLists.Add(listObj); - foreach(ShoppingListItem item in listObj.listItems) + Family familyObj = _dbContext.Families.Where(x => x.Id == familyId).FirstOrDefault(); + if (familyObj != null) { - _dbContext.ShoppingListItems.Add(item); + _dbContext.ShoppingLists.Add(listObj); + foreach (ShoppingListItem item in listObj.listItems) + { + _dbContext.ShoppingListItems.Add(item); + } + familyObj.shoppingList = listObj; + _dbContext.SaveChanges(); } - - _dbContext.SaveChanges(); } - catch + catch(Exception e) { - return NotFound("could not create a list"); + return NotFound("could not create a list : " + e.Message); } return Ok(listObj); @@ -109,15 +117,15 @@ public IActionResult UpdateList([FromBody]ShoppingList updatedListObj, int listI try { listObj = _dbContext.ShoppingLists.Where(x => x.Id == listId).FirstOrDefault(); - if(listObj != null) + if(listObj != null && listObj.Id == updatedListObj.Id) { _dbContext.ShoppingLists.Update(updatedListObj); _dbContext.SaveChanges(); } } - catch + catch(Exception e) { - return NotFound("could not update a list " + listId); + return NotFound("could not update a list : " + e.Message); } return Ok(updatedListObj); diff --git a/MirysList/Models/Family.cs b/MirysList/Models/Family.cs index 75c7466..898feda 100644 --- a/MirysList/Models/Family.cs +++ b/MirysList/Models/Family.cs @@ -45,5 +45,7 @@ public class Family public string PhotoUrl { get; set; } public virtual ICollection FamilyMembers {get; set;} + + public ShoppingList shoppingList { get; set; } } } diff --git a/MirysList/Models/ShoppingList.cs b/MirysList/Models/ShoppingList.cs index 211692c..895a93a 100644 --- a/MirysList/Models/ShoppingList.cs +++ b/MirysList/Models/ShoppingList.cs @@ -10,7 +10,7 @@ public class ShoppingList { [Required] public int Id { get; set; } - public Family Family { get; set; } + public string Title { get; set; } public List listItems { get; set; } } } diff --git a/MirysList/appsettings.json b/MirysList/appsettings.json index c5ba93b..994b859 100644 --- a/MirysList/appsettings.json +++ b/MirysList/appsettings.json @@ -13,7 +13,7 @@ } }, "ConnectionStrings": { - "DbContextConnectionString": "Data Source=tcp:miryslistserver.database.windows.net,1433;Initial Catalog=MirysListDb;User ID=miryslistserver;Password=Hackathon123" + "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" } }