Skip to content

Commit

Permalink
Merge pull request #6 from richa008/swapnag-backendservice
Browse files Browse the repository at this point in the history
fixing post api
  • Loading branch information
swapnaguddanti authored Jul 25, 2018
2 parents 9372a61 + c84ef9d commit c45ba39
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
2 changes: 1 addition & 1 deletion MirysList/Controllers/FamilyController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public IActionResult GetFamilies(string sort)
[Route("families/{id}")]
public IActionResult GetFamily(int id)
{
var family = _dbContext.Families.Include(p => p.FamilyMembers).SingleOrDefault(m => m.Id == id);
var family = _dbContext.Families.Include(p => p.FamilyMembers).Include(i => i.listItems).SingleOrDefault(m => m.Id == id);
if (family == null)
{
return NotFound("No record found with this Id");
Expand Down
30 changes: 23 additions & 7 deletions MirysList/Controllers/ShopController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public IActionResult Catalog()
public IActionResult CataLogItems(int catalogId)
{


Catalog catalog = _dbContext.Catalogs.Where(x => x.Id == catalogId).Include(c => c.Items).FirstOrDefault();
if (catalog != null)
{
Expand All @@ -59,9 +60,16 @@ public IActionResult ShoppingList(int familyId)
if (familyObj != null)
{
List<ShoppingListItem> listItems = familyObj.listItems;
List<ShoppingListItem> resultItems = new List<ShoppingListItem>();
if (listItems != null)
{
return Ok(listItems);
foreach(ShoppingListItem item in listItems)
{
ShoppingListItem it = _dbContext.ShoppingListItems.Where(x => x.Id == item.Id).FirstOrDefault();
resultItems.Add(it);
}

return Ok(resultItems);
}
}

Expand Down Expand Up @@ -93,21 +101,29 @@ public IActionResult ShoppingListItems(int shoppinglistId)
[Route("api/Shop/CreateList/{familyId}")]
public IActionResult CreateList([FromBody]List<ShoppingListItem> listItems, int familyId)
{
List<UpdatedShoppingListItem> result = new List<UpdatedShoppingListItem>();
Family familyObj = null;
try
{
familyObj = _dbContext.Families.Where(x => x.Id == familyId).FirstOrDefault();
familyObj = _dbContext.Families.Where(x => x.Id == familyId).Include(y => y.listItems).FirstOrDefault();
if (familyObj != null)
{
foreach (ShoppingListItem item in listItems)
{
_dbContext.ShoppingListItems.Add(item);
familyObj.listItems.Add(item);
_dbContext.Families.Update(familyObj);
CatalogItem i = _dbContext.CataLogItems.Where(x => x.Id == item.CatalogItemId).FirstOrDefault();
if (i != null)
{
_dbContext.ShoppingListItems.Add(item);
UpdatedShoppingListItem resultItem = new UpdatedShoppingListItem(item);
resultItem.CatalogItem = i;
familyObj.listItems.Add(item);
result.Add(resultItem);
}
}


_dbContext.Families.Update(familyObj);
_dbContext.SaveChanges();
return Ok(familyObj);
return Ok(result);
}
}
catch(Exception e)
Expand Down
29 changes: 26 additions & 3 deletions MirysList/Models/ShoppingListItem.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,44 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Threading.Tasks;

namespace MirysList.Models
{
public class ShoppingListItem
{
public ShoppingListItem()
{

}

public ShoppingListItem(ShoppingListItem item)
{
this.Id = item.Id;
this.CatalogItemId = item.CatalogItemId;
this.ItemNotes = item.ItemNotes;
this.Quantity = item.Quantity;
}

[Required]
public int Id { get; set; }
[Required]
public CatalogItem CatalogItem { get; set; }
public int Id { get; set; }
public int CatalogItemId { get; set; }
[Required]
public int Quantity { get; set; }
// public ShoppingList ShoppingList { get; set; }
//[Required]
public string ItemNotes { get; set; }
}

public class UpdatedShoppingListItem : ShoppingListItem
{
public UpdatedShoppingListItem(ShoppingListItem item) :base(item)
{

}

public CatalogItem CatalogItem { get; set; }
}
}

0 comments on commit c45ba39

Please sign in to comment.