// GET api/ListItem/GetItem/5 public API_ListItem GetItem(string id) { API_ListItem returnItem = new API_ListItem(); using (_dataMethods = new DataMethods()) { var item = _dataMethods.ListItem_GetByPublicKey(id); if (item == null) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound)); } else { returnItem = returnItem.ConvertToAPI_ListItem(item, item.List.PublicKey); } } return returnItem; }
private static Item CreateListItem(string listPublicKey, Item newItem) { GyftoList.API.Translations.API_ListItem newListItem = new API.Translations.API_ListItem(); var gyftoApi = new API_ListItem(); var translatedListItem = gyftoApi.ConvertToAPI_ListItem(newItem, listPublicKey); Uri apiURI = null; HttpResponseMessage response = client.PostAsJsonAsync("api/listitem/postitem", translatedListItem).Result; if (!response.IsSuccessStatusCode) { throw new Exception("Error"); } else { apiURI = response.Headers.Location; } return newItem; }
private static Item GetListItemByPublicKey(string listItemPublicKey) { GyftoList.API.Translations.API_ListItem newListItem = new API.Translations.API_ListItem(); var gyftoApi = new API_ListItem(); Item translatedListItem = null; var requestURI = string.Format("api/ListItem/GetItem/{0}",listItemPublicKey); HttpResponseMessage response = client.GetAsync(requestURI.ToString()).Result; if (!response.IsSuccessStatusCode) { throw new Exception("Error"); } else { translatedListItem = gyftoApi.ConvertFromAPI_ListItem(response.Content.ReadAsAsync<API_ListItem>().Result); } return translatedListItem; }
/// <summary> /// Creates an List object with associated List Items from an API_List object /// </summary> /// <param name="inputList"></param> /// <returns></returns> public List ConvertFromAPI_List(API_List inputList) { // First get the base list var rcList = new List() { Description = inputList.Description , PublicKey = inputList.PublicKey , Title = inputList.ListName }; // Next, add converted Items if (inputList.Items.Count > 0) { var apiListItem = new API_ListItem(); foreach (var i in inputList.Items) { rcList.Items.Add(apiListItem.ConvertFromAPI_ListItem(i)); } } // Finally, add converted ListShares // TODO: ADD 'CONVERT FROM' FOR API_ListShare if (inputList.ListShares.Count > 0) { var apiListShare = new API_ListShare(); foreach(var s in inputList.ListShares) { } } return rcList; }
/// <summary> /// This method converts a List to an API_List object. /// </summary> /// <param name="list"></param> /// <returns></returns> public API_List ConvertToAPI_ListWithAllItems(List list) { var rcList = _CreateShellList(list); try { if ((rcList != null) && (list.Items != null) && (list.Items.Count > 0)) { API_ListItem apiListItem = new API_ListItem(); foreach (var i in list.Items.OrderBy(li => li.Ordinal)) { rcList.Items.Add(apiListItem.ConvertToAPI_ListItem(i, rcList.PublicKey)); } } } catch (ObjectDisposedException) { } return rcList; }
// POST api/ListItem/PostItem/ public HttpResponseMessage PostItem(API_ListItem item) { if (ModelState.IsValid) { var createdItem = new Item(); try { using (_dataMethods = new DataMethods()) { // Convert from the API to the Item var listItem = converter.ConvertFromAPI_ListItem(item); // Create the item createdItem = _dataMethods.ListItem_Create(item.ListPublicKey, listItem.Title, listItem.Description, listItem.Cost, listItem.CostRangeStart, listItem.CostRangeEnd, listItem.Size, listItem.Color, listItem.Qty, listItem.Ordinal, listItem.ImageURL, listItem.ItemURL); // Next, create an ItemShare for the item for all the associated ListShares foreach (var ls in _dataMethods.ListShare_GetAllByListPublicKey(item.ListPublicKey)) { _dataMethods.ItemShare_Create(ls.ListShareID, item.ItemID); } } } catch (Exception) { throw; } HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, item); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = createdItem.PublicKey })); return response; } else { return Request.CreateResponse(HttpStatusCode.BadRequest); } }
public Item ConvertFromAPI_ListItem(API_ListItem apiListItem) { // Create Date, Created By, return new Item { PublicKey = apiListItem.PublicKey, Active = apiListItem.Active, Color = apiListItem.Color, Cost = apiListItem.Cost, CostRangeEnd = apiListItem.CostRangeEnd, CostRangeStart = apiListItem.CostRangeStart, Description = apiListItem.Description, ImageURL = apiListItem.ImageURL, ItemURL = apiListItem.ItemURL, Ordinal = apiListItem.Ordinal, Qty = apiListItem.Qty, Size = apiListItem.Size, Title = apiListItem.Title }; }