Skip to content

Commit

Permalink
Merge pull request #5 from richa008/swapnag-backendservice
Browse files Browse the repository at this point in the history
fixed models
  • Loading branch information
swapnaguddanti authored Jul 24, 2018
2 parents 4ecc19f + fad6989 commit 9372a61
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 45 deletions.
89 changes: 51 additions & 38 deletions MirysList/Controllers/ShopController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,19 @@ public IActionResult Catalog()
[HttpGet]
[Route("api/Shop/Catalog/{catalogId}")]
public IActionResult CataLogItems(int catalogId)
{
IQueryable<CatalogItem> 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<CatalogItem> cItems = catalog.Items;
if (cItems != null)
{
return Ok(cItems);
}
}

return NotFound("could not find any catalog items for catalogid " + catalogId);
}

// GET: api/Shop/List
Expand All @@ -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<ShoppingListItem> listItems = familyObj.listItems;
if (listItems != null)
{
return Ok(listObj);
return Ok(listItems);
}
}

Expand All @@ -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<ShoppingListItem> 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<ShoppingListItem> 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<ShoppingListItem> 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<ShoppingListItem> 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);
}
}
}
1 change: 1 addition & 0 deletions MirysList/MirysList.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<ItemGroup>
<Compile Remove="Models\MirysListModels.cs" />
<Compile Remove="Models\ShoppingList.cs" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion MirysList/Models/AppDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
public DbSet<Catalog> Catalogs { get; set; }
public DbSet<CatalogItem> CataLogItems { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<ShoppingList> ShoppingLists { get; set; }
// public DbSet<ShoppingList> ShoppingLists { get; set; }
public DbSet<ShoppingListItem> ShoppingListItems { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<UserRole> UserRoles { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion MirysList/Models/CatalogItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
}
2 changes: 1 addition & 1 deletion MirysList/Models/Family.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ public class Family

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

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

0 comments on commit 9372a61

Please sign in to comment.