<commit_before>﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Security.Principal;
using System.Web.Mvc;
using System.Web.Security;
using System.Web.UI.WebControls;
using BarUTomaModels.Models;
using BarUTomaREST.Controllers;
using Microsoft.AspNet.Identity;

namespace BarUTomaREST.Models
{
    public class BarRepository : Repository<Bar>
    {
        public BarRepository(DbContext db)
            : base(db)
        {
        }

        public bool IsDrinkAvailable(Bar bar, Drink drink)
        {
            if (db.Set<DrinkBar>().Count(x => x.Bar == bar && x.Drink == drink) == 0) //drink is not offered at all
            {
                return false;
            }

            drink.IngredientsUsed.Select(x => new Tuple<Ingredient, Quantity>(x.Ingredient, x.Quantity)).ToList();

            foreach (var ingredient in drink.IngredientsUsed)
            {
                if (!bar.IngredientsAvailable.Any(x => x.Item1.Equals(ingredient.Ingredient)))
                {
                    return false;
                }
                var result = bar.IngredientsAvailable.Where(x => x.Item1.Equals(ingredient.Ingredient))
                    .Select(x => x.Item2.Amount)
                    .First() >= ingredient.Quantity.Amount;
                if (!result)
                {
                    return false;
                }
            }
            return true;
        }

        public List<Tuple<Drink, bool>> ListBarDrinks(Bar bar)
        {
            return
                db.Set<DrinkBar>().Where(x => x.Bar.Equals(bar)).Select(x => new Tuple<Drink, bool>(x.Drink, IsDrinkAvailable(bar, x.Drink))).ToList();
        }

        public void AddNewBar(Bar bar, IIdentity user)
        {
            if (bar == null)
            {
                throw new ArgumentNullException("bar");
            }

            if (FindAll().Contains(bar))
            {
                throw new ArgumentException("Bar already exists!"); 
            }

            ApplicationUser newUser =
                db.Set<ApplicationUser>().First(x => x.Id.Equals(user.GetUserId()));

            UserBar userBar = new UserBar {User = newUser, Bar = bar, UserRole = new UserRole(UserBarRepository.ADMIN_ROLE)};
            userBar.UserRole.Users.Add(newUser);

            bar.Users.Add(userBar);

            Add(bar);
            Save();
        }

        public void EditBar(Bar editedBar)
        {
            if (editedBar == null)
            {
                throw new ArgumentNullException("bar");
            }

            Bar existingBar = FindByPK(editedBar.BarId);

            if (existingBar == null)
            {
                throw new ArgumentException("bar");
            }

            existingBar.Address = editedBar.Address;
            existingBar.BarType = editedBar.BarType;
            existingBar.Drinks = editedBar.Drinks;
            existingBar.DrinksOnBar = editedBar.DrinksOnBar;
            existingBar.Events = editedBar.Events;
            existingBar.Info = editedBar.Info;
            existingBar.IngredientsAvailable = editedBar.IngredientsAvailable;
            existingBar.Name = editedBar.Name;
            existingBar.Orders = editedBar.Orders;
            existingBar.Users = editedBar.Users;

            Save();
        }
    }
}<commit_msg>Added DateCreated and DateModified property to Bar<commit_after>﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Security.Principal;
using System.Web.Mvc;
using System.Web.Security;
using System.Web.UI.WebControls;
using BarUTomaModels.Models;
using BarUTomaREST.Controllers;
using Microsoft.AspNet.Identity;

namespace BarUTomaREST.Models
{
    public class BarRepository : Repository<Bar>
    {
        public BarRepository(DbContext db)
            : base(db)
        {
        }

        public bool IsDrinkAvailable(Bar bar, Drink drink)
        {
            if (db.Set<DrinkBar>().Count(x => x.Bar == bar && x.Drink == drink) == 0) //drink is not offered at all
            {
                return false;
            }

            drink.IngredientsUsed.Select(x => new Tuple<Ingredient, Quantity>(x.Ingredient, x.Quantity)).ToList();

            foreach (var ingredient in drink.IngredientsUsed)
            {
                if (!bar.IngredientsAvailable.Any(x => x.Item1.Equals(ingredient.Ingredient)))
                {
                    return false;
                }
                var result = bar.IngredientsAvailable.Where(x => x.Item1.Equals(ingredient.Ingredient))
                    .Select(x => x.Item2.Amount)
                    .First() >= ingredient.Quantity.Amount;
                if (!result)
                {
                    return false;
                }
            }
            return true;
        }

        public List<Tuple<Drink, bool>> ListBarDrinks(Bar bar)
        {
            return
                db.Set<DrinkBar>().Where(x => x.Bar.Equals(bar)).Select(x => new Tuple<Drink, bool>(x.Drink, IsDrinkAvailable(bar, x.Drink))).ToList();
        }

        public void AddNewBar(Bar bar, IIdentity user)
        {
            if (bar == null)
            {
                throw new ArgumentNullException("bar");
            }

            if (FindAll().Contains(bar))
            {
                throw new ArgumentException("Bar already exists!"); 
            }

            ApplicationUser newUser =
                db.Set<ApplicationUser>().First(x => x.Id.Equals(user.GetUserId()));

            UserBar userBar = new UserBar {User = newUser, Bar = bar, UserRole = new UserRole(UserBarRepository.ADMIN_ROLE)};
            userBar.UserRole.Users.Add(newUser);

            bar.Users.Add(userBar);

            bar.DateCreated = DateTime.Now;
            bar.DateModified = bar.DateCreated;

            Add(bar);
            Save();
        }

        public void EditBar(Bar editedBar)
        {
            if (editedBar == null)
            {
                throw new ArgumentNullException("bar");
            }

            Bar existingBar = FindByPK(editedBar.BarId);

            if (existingBar == null)
            {
                throw new ArgumentException("bar");
            }

            existingBar.Address = editedBar.Address;
            existingBar.BarType = editedBar.BarType;
            existingBar.Drinks = editedBar.Drinks;
            existingBar.DrinksOnBar = editedBar.DrinksOnBar;
            existingBar.Events = editedBar.Events;
            existingBar.Info = editedBar.Info;
            existingBar.IngredientsAvailable = editedBar.IngredientsAvailable;
            existingBar.Name = editedBar.Name;
            existingBar.Orders = editedBar.Orders;
            existingBar.Users = editedBar.Users;
            existingBar.DateModified = DateTime.Now;

            Save();
        }
    }
}
{'fraction_non_alphanumeric': 0.10314356077067942, 'fraction_numerical': 0.0011589164131537012, 'mean_word_length': 1.6811650485436893, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5882583', 'n_tokens_mistral': 1933, 'n_tokens_neox': 1840, 'n_words': 358}