Esempio n. 1
0
        public async Task <Fermentable> SaveFermentable(Fermentable fermentable, string userEmail)
        {
            var db = _mongoClient.GetDatabase("BeerDb");
            var mongoCollection = db.GetCollection <FermentableCollection>("Fermentables");
            var query           = Builders <FermentableCollection> .Filter.Eq(f => f.UserEmail, userEmail);

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

            FermentableCollection usersCollection = null;

            if (fermCollections == null || fermCollections.Count == 0)
            {
                usersCollection = CreateNewFermentableCollection(userEmail, mongoCollection);
            }
            else
            {
                //should only be one collection per user
                usersCollection = fermCollections[0];
            }
            var fermToUpdate = usersCollection.Fermentables.SingleOrDefault(f => f.Id == fermentable.Id);

            if (fermToUpdate == null)
            {
                fermentable.Id = Guid.NewGuid();
            }
            else
            {
                usersCollection.Fermentables.Remove(fermToUpdate);
            }
            usersCollection.Fermentables.Add(fermentable);
            var filter = Builders <FermentableCollection> .Filter.Eq(f => f.Id, usersCollection.Id);

            mongoCollection.ReplaceOne(filter, usersCollection);
            return(fermentable);
        }
Esempio n. 2
0
        private FermentableCollection CreateNewFermentableCollection(string email, IMongoCollection <FermentableCollection> collection)
        {
            var col = new FermentableCollection
            {
                UserEmail    = email,
                Fermentables = DefaultLists.GetDefaultFermentableCollection().ToList()
            };

            collection.InsertOne(col);
            return(col);
        }
Esempio n. 3
0
        public async Task <IEnumerable <Fermentable> > GetAllFermentablesForUser(string userEmail)
        {
            var db    = _mongoClient.GetDatabase("BeerDb");
            var query = Builders <FermentableCollection> .Filter.Eq(f => f.UserEmail, userEmail);

            var mongoCollection = db.GetCollection <FermentableCollection>("Fermentables");
            var fermCollections = await mongoCollection.Find(query).ToListAsync();

            FermentableCollection col = null;

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

            return(col.Fermentables);
        }