public JObject Get() { try { List <Distributor> List = DistributorsCollection.Find(new BsonDocument()).ToList(); return (JObject.FromObject( new { status = "success", result = List } )); } catch (Exception ex) { return (JObject.FromObject( new { status = "Exception Thrown", result = false, message = ex.Message } )); } }
public JObject Get(ObjectId Id) { try { var queryable = DistributorsCollection.AsQueryable(); var query = from p in queryable where p.Id.Equals(Id) select p; Distributor distributor = query.FirstOrDefault(); return (JObject.FromObject( new { status = "success", result = distributor } )); } catch (Exception ex) { return (JObject.FromObject( new { status = "Exception Thrown", result = false, message = ex.Message } )); } }
private Distributor GetDistributorById(string id) { var queryable = DistributorsCollection.AsQueryable(); var query = from p in queryable where p.Id.Equals(ObjectId.Parse(id)) select p; return(query.First()); }
private List <Line> ReturnInventoryForDistributor(string distributorId) { var queryable = DistributorsCollection.AsQueryable(); var inventoryQuery = from p in queryable where p.Id.Equals(ObjectId.Parse(distributorId)) select new { p.Inventory }; var inventoryList = inventoryQuery.First().Inventory; return(inventoryList); }
public async Task <JObject> AddOfferToDistributer(string distributorId, Offers offer) { try { Distributor distributor = GetDistributorById(distributorId); var offers = distributor.Offers; offers.Add(offer); var filter = Builders <Distributor> .Filter.Eq("_id", ObjectId.Parse(distributorId)); var update = Builders <Distributor> .Update.Set("Offers", offers); UpdateResult result = await DistributorsCollection.UpdateOneAsync(filter, update); if (result.IsModifiedCountAvailable) { return (JObject.FromObject( new { status = "success", result = true, message = "Offer added to distributor." } )); } else { return (JObject.FromObject( new { status = "false", result = false, message = "Could not update distributor with new offer." } )); } } catch (Exception ex) { return (JObject.FromObject( new { status = "Exception Thrown", result = false, message = ex.Message } )); } }
/* ***************** * -------------------------- * Helper Methods * ------------------------- */ protected Distributor GetDistributorById(string id) { try { var queryable = DistributorsCollection.AsQueryable(); var query = from p in queryable where p.Id.Equals(ObjectId.Parse(id)) select p; return(query.FirstOrDefault()); } catch (Exception ex) { throw new Exception(ex.Message, ex.InnerException); } }
public async Task <JObject> Create(Distributor item) { try { if (item.Offers == null) { item.Offers = new List <Offers>() { }; } if (item.ReceiptTypesOffered == null) { item.ReceiptTypesOffered = new List <ReceiptType>() { }; } if (item.Inventory == null) { item.Inventory = new List <Line>() { }; } await DistributorsCollection.InsertOneAsync(item); return (JObject.FromObject( new { status = "success", result = item, message = "Distributor Created" } )); } catch (Exception ex) { return (JObject.FromObject( new { status = "Exception Thrown", result = false, message = ex.Message } )); } }
private async Task <JObject> UpdateDistributor(string distributorId, List <ReceiptType> recieptTypes) { try { var filter = Builders <Distributor> .Filter.Eq("_id", ObjectId.Parse(distributorId)); var update = Builders <Distributor> .Update.Set("ReceiptTypesOffered", recieptTypes); UpdateResult result = await DistributorsCollection.UpdateOneAsync(filter, update); if (result.IsModifiedCountAvailable) { return (JObject.FromObject( new { status = "success", result = recieptTypes, message = "Distributor's Receipt Types Updated" } )); } else { return (JObject.FromObject( new { status = "false", result = false, message = "Could not add receipt type." } )); } } catch (Exception ex) { return (JObject.FromObject( new { status = "Exception Thrown", result = false, message = ex.Message })); } }
private async Task <JObject> UpdateInventoryList(string distributorId, List <Line> inventory) { try { var filter = Builders <Distributor> .Filter.Eq("_id", ObjectId.Parse(distributorId)); var update = Builders <Distributor> .Update.Set("Inventory", inventory); UpdateResult result = await DistributorsCollection.UpdateOneAsync(filter, update); if (result.IsModifiedCountAvailable) { return (JObject.FromObject( new { status = "success", result = inventory, message = "Inventory Updated" } )); } else { return (JObject.FromObject( new { status = "false", result = false, message = "Could not update inventory." } )); } } catch (Exception ex) { return (JObject.FromObject( new { status = "Exception Thrown", result = false, message = ex.Message })); } }
public async Task <JObject> Delete(string itemId) { try { var filter = Builders <Distributor> .Filter.Eq("_id", ObjectId.Parse(itemId)); DeleteResult result = await DistributorsCollection.DeleteOneAsync(filter); if (result.IsAcknowledged) { return (JObject.FromObject( new { status = "success", result = true, message = "Distributor was deleted" } )); } else { return (JObject.FromObject( new { status = "false", result = false, message = "Distributor could not be deleted" } )); } } catch (Exception ex) { return (JObject.FromObject( new { status = "Exception Thrown", result = false, message = ex.Message } )); } }
public async Task <JObject> AddNewLineToInventory(string distributorId, Line line) { try { line.Id = ObjectId.GenerateNewId(DateTime.Now).ToString(); line.Bike.Id = ObjectId.GenerateNewId(DateTime.Now).ToString(); var queryable = DistributorsCollection.AsQueryable(); var inventoryQuery = from p in queryable where p.Id.Equals(ObjectId.Parse(distributorId)) select new { p.Inventory }; List <Line> inventoryList; inventoryList = inventoryQuery.First().Inventory; if (inventoryList != null) { inventoryList.Add(line); } else { inventoryList = new List <Line>() { line }; } return(await this.UpdateInventoryList(distributorId, inventoryList)); } catch (Exception ex) { return (JObject.FromObject( new { status = "false", result = false, message = "Could not update inventory. Line item in inventory not found." } )); } }
public async Task <JObject> RemoveOfferFromDistributor(string distributorId, string offerId) { try { Offers offer = GetOfferById(offerId); //get distributor Distributor distributor = GetDistributorById(distributorId); List <Offers> offersList = distributor.Offers; var found = false; //check to see if the offer is already in the list of offers under the distributor foreach (var loffer in offersList.Where(loffer => loffer.Id == offerId)) { found = true; offersList.Remove(loffer); break; } if (found) { var filter = Builders <Distributor> .Filter.Eq("_id", ObjectId.Parse(distributorId)); //Remove not push var update = Builders <Distributor> .Update.Set("Offers", offersList); //add offer to distributor UpdateResult result = await DistributorsCollection.UpdateOneAsync(filter, update); if (result.IsModifiedCountAvailable) { return (JObject.FromObject( new { status = "success", result = true, message = "Offer removed to distributor" } )); } else { return (JObject.FromObject( new { status = "false", result = false, message = "Offer could not be removed to distributor" } )); } } else { return (JObject.FromObject( new { status = "false", result = false, message = "Offer could not be found on Distributor" } )); } } catch (Exception ex) { return (JObject.FromObject( new { status = "Exception Thrown", result = false, message = ex.Message } )); } }
public async Task <JObject> AddOfferToDistributor(string distributorId, string offerId) { try { Offers offer = GetOfferById(offerId); //get distributor Distributor distributor = GetDistributorById(distributorId); var found = false; //check to see if the offer is already in the list of offers under the distributor distributor.Offers.ForEach(delegate(Offers Loffer) { if (Loffer.Id == offer.Id) { found = true; } }); if (found) { return(JObject.FromObject( new { status = "success", result = true, message = "Offer has alredy been added" } )); } var filter = Builders <Distributor> .Filter.Eq("_id", ObjectId.Parse(distributorId)); var update = Builders <Distributor> .Update.Push("Offers", offer); //add offer to distributor UpdateResult result = await DistributorsCollection.UpdateOneAsync(filter, update); if (result.IsModifiedCountAvailable) { return (JObject.FromObject( new { status = "success", result = true, message = "Offer added to distributor" } )); } else { return (JObject.FromObject( new { status = "false", result = false, message = "Offer could not be added to distributor" } )); } } catch (Exception ex) { return (JObject.FromObject( new { status = "Exception Thrown", result = false, message = ex.Message } )); } }