示例#1
0
        public async Task <Hop> SaveHop(Hop hop, string userEmail)
        {
            var db = _mongoClient.GetDatabase("BeerDb");
            var mongoCollection = db.GetCollection <HopsCollection>("Hops");
            var query           = Builders <HopsCollection> .Filter.Eq(f => f.UserEmail, userEmail);

            var hopsCollections = await mongoCollection.Find(query).ToListAsync();

            HopsCollection usersCollection = null;

            if (hopsCollections == null || hopsCollections.Count == 0)
            {
                usersCollection = CreateNewHopsCollection(userEmail, mongoCollection);
            }
            else
            {
                //should only be one collection per user
                usersCollection = hopsCollections[0];
            }
            var hopToUpdate = usersCollection.Hops.SingleOrDefault(f => f.Id == hop.Id);

            if (hopToUpdate == null)
            {
                hop.Id = Guid.NewGuid();
            }
            else
            {
                usersCollection.Hops.Remove(hopToUpdate);
            }
            usersCollection.Hops.Add(hop);
            var filter = Builders <HopsCollection> .Filter.Eq(f => f.Id, usersCollection.Id);

            mongoCollection.ReplaceOne(filter, usersCollection);
            return(hop);
        }
示例#2
0
        private HopsCollection CreateNewHopsCollection(string email, IMongoCollection <HopsCollection> collection)
        {
            var col = new HopsCollection
            {
                UserEmail = email,
                Hops      = DefaultLists.GetDefaultHopsCollection().ToList()
            };

            collection.InsertOne(col);
            return(col);
        }
示例#3
0
        public async Task <IEnumerable <Hop> > GetAllHopsForUser(string userEmail)
        {
            var db    = _mongoClient.GetDatabase("BeerDb");
            var query = Builders <HopsCollection> .Filter.Eq(f => f.UserEmail, userEmail);

            var mongoCollection = db.GetCollection <HopsCollection>("Hops");
            var hopsCollections = await mongoCollection.Find(query).ToListAsync();

            HopsCollection col = null;

            if (hopsCollections == null || hopsCollections.Count == 0)
            {
                col = CreateNewHopsCollection(userEmail, mongoCollection);
            }
            else
            {
                //should only be one collection per user
                col = hopsCollections[0];
            }

            return(col.Hops);
        }