Skip to content

Commit

Permalink
Merge pull request #4 from richa008/swapnag-backendservice
Browse files Browse the repository at this point in the history
fixing api and models
  • Loading branch information
swapnaguddanti authored Jul 24, 2018
2 parents 4b9142e + 13967b3 commit 4ecc19f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
38 changes: 23 additions & 15 deletions MirysList/Controllers/ShopController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions MirysList/Models/Family.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,7 @@ public class Family
public string PhotoUrl { get; set; }

public virtual ICollection<User> FamilyMembers {get; set;}

public ShoppingList shoppingList { get; set; }
}
}
2 changes: 1 addition & 1 deletion MirysList/Models/ShoppingList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ShoppingListItem> listItems { get; set; }
}
}
2 changes: 1 addition & 1 deletion MirysList/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

0 comments on commit 4ecc19f

Please sign in to comment.