// POST: api/GetItem
        /// <summary>
        /// GetItem requests an Item by id
        /// </summary>
        /// <param name="request"></param>
        /// <returns>GetItemResponse</returns>
        public GetItemsResponse Post(GetItemsRequest request)
        {
            var response = new GetItemsResponse {
            };

            var repo = new RestfulApiRepo();

            try
            {
                foreach (var itemId in request.ItemIds)
                {
                    var query = repo.GetItem(itemId);
                    response.Items.Add(query.AsItemDTO());
                }
            }
            catch (Exception ex)
            {
                response.Fail("GetItem", ex);
            }
            finally
            {
                repo.Dispose();
            }

            response.End();
            return(response);
        }
        // POST: api/UpdateCollections

        /// <summary>
        /// UpdateItemsRequest takes a list of items to be updated
        /// </summary>
        /// <param name="request"></param>
        /// <returns>UpdateItemsResponse</returns>
        public UpdateItemsResponse Post(UpdateItemsRequest request)
        {
            var response = new UpdateItemsResponse {
            };
            var repo     = new RestfulApiRepo();

            try
            {
                var itemsGroupedByCollection = request.Items.GroupBy(i => i.ExampleCollectionId).ToList();
                foreach (var collectionItemGrouping in itemsGroupedByCollection)
                {
                    foreach (var item in collectionItemGrouping)
                    {
                        switch (item.UpdateType)
                        {
                        case UpdateType.IsCreate:
                            var itemId = repo.CreateItem(item.ExampleCollectionId, item.ItemBool, item.ItemInt, "" + item.ItemString);
                            response.UpdatedItemIds.Add(new UpdatedItem {
                                OrigionalId = item.ExampleItemId, NewId = itemId
                            });
                            break;

                        case UpdateType.IsUpdate:
                            repo.UpdateItem(
                                item.ExampleItemId,
                                item.ItemBool,
                                item.ItemInt,
                                item.ItemString
                                );
                            response.UpdatedItemIds.Add(new UpdatedItem {
                                OrigionalId = item.ExampleItemId, NewId = item.ExampleItemId
                            });
                            break;

                        case UpdateType.IsDelete:
                            repo.DeleteItem(item.ExampleItemId);
                            response.UpdatedItemIds.Add(new UpdatedItem {
                                OrigionalId = item.ExampleItemId, NewId = null
                            });
                            break;

                        default:
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                response.Fail("UpdateItems", ex);
            }
            finally
            {
                repo.Dispose();
            }

            response.End();
            return(response);
        }
        // POST: api/GetCollection
        /// <summary>
        ///  GetCollection requests a collection and its items
        /// </summary>
        /// <param name="request"></param>
        /// <returns>GetCollectionResponse</returns>
        public GetCollectionResponse Post(GetCollectionRequest request)
        {
            var response = new GetCollectionResponse {
            };

            var repo = new RestfulApiRepo();

            try
            {
                var query = repo.GetCollection(request.ExampleCollectionId, request.IncludeItems);
                response.Collection = query.AsItemCollectionDTO(request.IncludeItems);
            }
            catch (Exception ex)
            {
                response.Fail("GetCollection", ex);
            }
            finally
            {
                repo.Dispose();
            }

            response.End();
            return(response);
        }
Beispiel #4
0
        // POST: api/GetCollections
        /// <summary>
        /// GetCollections can request specific collections, if none are specified it will return all.
        /// It can also specify if it should include the items as part of the request.
        /// </summary>
        /// <param name="request"></param>
        /// <returns>GetCollectionsResponse</returns>
        public GetCollectionsResponse Post(GetCollectionsRequest request)
        {
            var response = new GetCollectionsResponse {
            };

            var repo = new RestfulApiRepo();

            try
            {
                List <ExampleCollection> query = repo.GetCollections(request.CollectionIds, request.IncludeItems);
                response.Collections = query.Select(i => i.AsItemCollectionDTO(request.IncludeItems)).ToList();
            }
            catch (Exception ex)
            {
                response.Fail("GetCollections", ex);
            }
            finally
            {
                repo.Dispose();
            }

            response.End();
            return(response);
        }
Beispiel #5
0
        // POST: api/UpdateCollections
        /// <summary>
        /// UpdateCollections takes a list of collections that need to be updated. Does not update collection items.
        /// </summary>
        /// <param name="request"></param>
        /// <returns>UpdateCollectionsResponse</returns>
        public UpdateCollectionsResponse Post(UpdateCollectionsRequest request)
        {
            var response = new UpdateCollectionsResponse {
            };

            var repo = new RestfulApiRepo();

            try
            {
                if (request.Collections != null && request.Collections.Count > 0)
                {
                    foreach (var collectionDTO in request.Collections)
                    {
                        var collection = repo.GetCollection(collectionDTO.ExampleCollectionId);

                        // check
                        switch (collectionDTO.UpdateType)
                        {
                        case UpdateType.IsCreate:
                            // create the collection
                            var newCollectionId = repo.CreateCollection(collectionDTO.Name);

                            // create the updated collection response
                            var createdCollection = new UpdatedCollection {
                                OrigionalId = collectionDTO.ExampleCollectionId, NewId = newCollectionId
                            };

                            // check if any items need to be updated along with the collection
                            if (collectionDTO.ItemDTOs != null)
                            {
                                var items = collectionDTO.ItemDTOs.Select(i => new ItemDTO
                                {
                                    ExampleCollectionId = newCollectionId,
                                    ItemString          = i.ItemString,
                                    ItemBool            = i.ItemBool,
                                    ItemInt             = i.ItemInt
                                }).ToList();

                                var updatedItemsResponse = (new UpdateItemsController()).Post(new UpdateItemsRequest {
                                    RequestTime = request.RequestTime,
                                    Items       = items.ToList()
                                });
                                // update the response list for the items too
                                createdCollection.UpdatedItemIds = updatedItemsResponse.UpdatedItemIds;
                            }

                            response.UpdatedCollectionIds.Add(createdCollection);

                            break;

                        case UpdateType.IsUpdate:
                            repo.UpdateCollection(collectionDTO.ExampleCollectionId, collectionDTO.Name);

                            var updatedCollection = new UpdatedCollection {
                                OrigionalId = collectionDTO.ExampleCollectionId,
                                NewId       = collectionDTO.ExampleCollectionId
                            };

                            // check if any items need to be updated along with the collection
                            if (collectionDTO.ItemDTOs != null)
                            {
                                var updatedItemsResponse = (new UpdateItemsController()).Post(new UpdateItemsRequest {
                                    RequestTime = request.RequestTime,
                                    Items       = collectionDTO.ItemDTOs.ToList()
                                });
                                // update the response list for the items too
                                updatedCollection.UpdatedItemIds = updatedItemsResponse.UpdatedItemIds;
                            }

                            response.UpdatedCollectionIds.Add(updatedCollection);
                            break;

                        case UpdateType.IsDelete:
                            repo.DeleteCollection(collectionDTO.ExampleCollectionId);
                            response.UpdatedCollectionIds.Add(new UpdatedCollection {
                                OrigionalId = collectionDTO.ExampleCollectionId,
                                NewId       = null
                            });
                            break;

                        default:
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                response.Fail("UpdateCollections", ex);
            }
            finally
            {
                repo.Dispose();
            }

            response.End();
            return(response);
        }