public Product ReceiveCoupon(ReceiveCoupon receiveInfo) { var result = CouponDB.ReceiveCoupon(receiveInfo); if (result != null) { return(result); } return(null); }
public static Product ReceiveCoupon(ReceiveCoupon receiveInfo) { db = GetMongoDatabase(); string UnUsedCoupon = null; var collection_AMZCoupon = db.GetCollection <Product>("AMZCoupon"); var _id = ObjectId.Parse(receiveInfo.ProductID); var builder = Builders <Product> .Filter; var filter = builder.And(builder.Eq("_id", _id)); var query = collection_AMZCoupon.Find(filter).ToList().FirstOrDefault(); foreach (var item in query.Coupons) { if (item["Used"] == "n") { UnUsedCoupon = item["Coupon"].ToString(); item["Used"] = "y"; break; } } if (UnUsedCoupon == null) { return(null); } var update = Builders <Product> .Update.Set("Coupons", query.Coupons); var updateResult = collection_AMZCoupon.UpdateOne(filter, update); //Insert User Info (Email and Name) var collectionMember = db.GetCollection <BsonDocument>("User"); var document = new BsonDocument { { "Name", receiveInfo.Name }, { "Email", receiveInfo.Email } }; collectionMember.InsertOne(document); query.Coupons = null; query.PCoupon = UnUsedCoupon; return(query); }