<commit_before>﻿namespace JustWars.Web
{
    using System;
    using System.Linq;
    using System.Linq.Dynamic;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    using Models;

    public partial class Shop : Page
    {
        private JustWarsDbContext dbcontext;
        private User user;
        private Item selectedItem;

        public Shop()
        {
            this.dbcontext = new JustWarsDbContext();
            this.user = this.dbcontext.Users.FirstOrDefault(u => u.UserName == this.User.Identity.Name);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.gold.InnerText = user.Gold.ToString();
        }

        public IQueryable<Item> GridViewItems_GetData()
        {
            return this.dbcontext.Items
                        .OrderBy(b => b.Gold);
        }

        protected void BuyItem(object sender, CommandEventArgs e)
        {
            var id = int.Parse(e.CommandArgument.ToString());
            this.selectedItem = this.dbcontext.Items.Find(id);

            if (this.user.Gold >= this.selectedItem.Gold)
            {
                this.user.Items.Add(this.selectedItem);
                this.user.Gold -= (int)this.selectedItem.Gold;
                this.dbcontext.SaveChanges();
            }
            else
            {
                this.error.Visible = true;
            }
        }
    }
}<commit_msg>User can no longer buy the same item twice<commit_after>﻿namespace JustWars.Web
{
    using System;
    using System.Linq;
    using System.Linq.Dynamic;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    using Models;

    public partial class Shop : Page
    {
        private JustWarsDbContext dbcontext;
        private User user;
        private Item selectedItem;

        public Shop()
        {
            this.dbcontext = new JustWarsDbContext();
            this.user = this.dbcontext.Users.FirstOrDefault(u => u.UserName == this.User.Identity.Name);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            this.gold.InnerText = user.Gold.ToString();
        }

        public IQueryable<Item> GridViewItems_GetData()
        {
            return this.dbcontext.Items
                        .OrderBy(b => b.Gold);
        }

        protected void BuyItem(object sender, CommandEventArgs e)
        {
            var id = int.Parse(e.CommandArgument.ToString());
            this.selectedItem = this.dbcontext.Items.Find(id);

            if (this.user.Gold < this.selectedItem.Gold)
            {
                this.error.Visible = true;
            }
            else if (this.user.Items.Contains(this.selectedItem))
            {
                this.error.InnerText = "You already have this item!";
                this.error.Visible = true;
            }
            else
            {
                this.error.Visible = false;
                this.user.Items.Add(this.selectedItem);
                this.user.Gold -= (int)this.selectedItem.Gold;
                this.dbcontext.SaveChanges();
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.09575795383655646, 'fraction_numerical': 0.0, 'mean_word_length': 1.5779742765273312, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 10, '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': '1533732', 'n_tokens_mistral': 880, 'n_tokens_neox': 852, 'n_words': 173}