public JObject Get(ObjectId itemId) { try { var queryableDiscount = DiscountCollection.AsQueryable(); var queryOffer = from d in queryableDiscount where d.Id.Equals(itemId) select d; var discount = queryableDiscount.First(); return (JObject.FromObject( new { status = "success", result = discount } )); } catch (Exception ex) { return (JObject.FromObject( new { status = "Exception Thrown", result = false, message = ex.Message } )); } }
public async Task <JObject> RemoveDiscountFromOffer(string offerId, string discountId) { try { //get discount var discountQueryable = DiscountCollection.AsQueryable(); var discountQuery = from d in discountQueryable where d.Id.Equals(ObjectId.Parse(discountId)) select d; Discount discount = discountQuery.First(); //get Offer Offers offer = GetOfferById(offerId); List <Discount> discounts = offer.Discounts; if (discounts.Contains(discount)) { discounts.Remove(discount); var filter = Builders <Offers> .Filter.Eq("_id", ObjectId.Parse(offerId)); var update = Builders <Offers> .Update.Set("Discounts", discounts); UpdateResult result = await OffersCollection.UpdateOneAsync(filter, update); if (result.IsModifiedCountAvailable) { return (JObject.FromObject( new { status = "success", result = true, message = "Added Discount to Offer" } )); } else { return (JObject.FromObject( new { status = "false", result = false, message = "Could not add Discount to Offer" } )); } } else { return (JObject.FromObject( new { status = "false", result = false, message = "That offer does not contain this discount" } )); } } catch (Exception ex) { return (JObject.FromObject( new { status = "Exception Thrown", result = false, message = ex.Message } )); } }