public void CopyFrom(ItemViewModel itemViewModel, DBUnitOfWork db) { //Id = itemViewModel.Id; Name = itemViewModel.Name; Description = itemViewModel.Description; Status = itemViewModel.Status; Location = db.Repository<Location>().Get(LocationId); if (itemViewModel.LocationId != null) { LocationId = itemViewModel.LocationId.Value; } else { //LocationId = itemViewModel.LocationId ?? new Guid("803ad3c6-cd87-4ad8-9a26-3675f4999f42"); var defaultLocation = db.Repository<Location>().Find(x => x.Name == "Unknown"); if (defaultLocation != null) { LocationId = defaultLocation.EntityId; } else { throw new Exception("You don't have the 'Unknown' location."); } } Quantity = itemViewModel.Quantity; MinQuantity = itemViewModel.MinQuantity; Relevance = itemViewModel.Relevance; Price = itemViewModel.Price; Picture = itemViewModel.Picture; Categories = new List<Category>(); if (itemViewModel.Categories != null) { foreach (var catId in itemViewModel.Categories) { var existing = db.Repository<Category>().Get(catId); if (existing != null) { Categories.Add(existing); } } } }
public HttpResponseMessage GetItem() { using (var db = new DBUnitOfWork()) { try { List<ItemViewModel> itemViewModelList = new List<ItemViewModel>(); var itemList = db.Repository<Item>().GetAll(); foreach (var item in itemList) { ItemViewModel itemViewModel = new ItemViewModel(); itemViewModel.CopyFrom(item); itemViewModelList.Add(itemViewModel); } return Request.CreateResponse(HttpStatusCode.OK, itemViewModelList); } catch (Exception) { return Request.CreateResponse(HttpStatusCode.InternalServerError); } } }