示例#1
0
        static void Main(string[] args)
        {
            var connectionString = ConfigurationManager.AppSettings["defaultConnection"];

            using (var context = new HeroDbContext(connectionString))
            {
                if (context.Database.Exists())
                    context.Database.Delete();

                context.Database.Create();
            }

            var contexts =
                new DbContext[]
                {
                };

            foreach (var context in contexts)
            {
                var adapter = (IObjectContextAdapter)context;

                var script = adapter.ObjectContext.CreateDatabaseScript();

                context.Database.ExecuteSqlCommand(script);

                context.Dispose();
            }

            //Seed Databases
            using (var context = new HeroDbContext(connectionString))
            {
                HeroDbContextInitializer.Seed(context);
            }
        }
示例#2
0
        public async Task <ActionResult> InsertVote([FromBody] string heroid)
        {
            string IPAddress  = Request.HttpContext.Connection.RemoteIpAddress.ToString();
            string heroDetail = "";

            try
            {
                using (HeroDbContext db = new HeroDbContext())
                {
                    SqlParameter[] parameters = new SqlParameter[] {
                        new SqlParameter("@HeroID", SqlDbType.VarChar, 5)
                        {
                            Value = heroid
                        },
                        new SqlParameter("@IPAddress", SqlDbType.VarChar, 30)
                        {
                            Value = IPAddress
                        }
                    };
                    var hero = await db.Votes.FromSql("EXEC InsertVote @HeroID, @IPAddress", parameters)
                               .Select(h => h.HeroID).AsNoTracking().FirstOrDefaultAsync();

                    heroDetail = JsonConvert.SerializeObject(hero);
                }
            }
            catch (Exception)
            {
                return(BadRequest());
            }
            return(Ok(heroDetail));
        }
示例#3
0
        //Check Username Password for Login
        private async Task <bool> GetUserIdFromCredentials(Account loginViewModel)
        {
            bool userId = false;

            try
            {
                using (HeroDbContext db = new HeroDbContext())
                {
                    DateTime date = DateTime.Now;

                    var userdb = await(from u in db.Admins where u.Username == loginViewModel.Username select u).FirstOrDefaultAsync();

                    if (userdb != null)
                    {
                        if ((loginViewModel.Username == userdb.Username) && (loginViewModel.Password == userdb.Password))
                        {
                            userId = true;
                        }
                    }
                }
            }
            catch (Exception) { }

            return(userId);
        }
示例#4
0
        public async Task <ActionResult> getTopHeroes()
        {
            string heroList = "";

            try
            {
                using (HeroDbContext db = new HeroDbContext())
                {
                    /*var hero = await (from h in db.Heroes
                     *          join v in db.Votes on h.HeroID equals v.HeroID into result1
                     *          from r1 in result1.DefaultIfEmpty() group r1 by new {h.HeroID,h.HeroName,h.HeroIMG} into result2
                     *          select new { result2.Key.HeroID, result2.Key.HeroName, result2.Key.HeroIMG,
                     *          HeroVoted = result2.Count(c => c != null) })*/

                    var hero = await(from h in db.Heroes
                                     let vCount = (from v in db.Votes where h.HeroID == v.HeroID select v).Count()
                                                  let cCount = (from c in db.Comments where h.HeroID == c.HeroID select c).Count()
                                                               select new { h.HeroID, h.HeroName,
                                                                            HeroIMG   = imgStorage + (h.HeroIMG != null && h.HeroIMG != "" ? h.HeroIMG : "noimages.jpg;"),
                                                                            HeroVoted = vCount, HeroCommented = cCount })
                               .OrderByDescending(h => h.HeroVoted).AsNoTracking().Take(5).ToListAsync();
                    heroList = JsonConvert.SerializeObject(hero);
                }
            }
            catch (Exception)
            {
                return(BadRequest());
            }
            return(Ok(heroList));
        }
 public PlayerHand Get(HeroDbContext dbContext)
 {
     return(new PlayerHand
     {
         Cards = dbContext.HeroCards.ToList()
     });
 }
示例#6
0
        protected override void Seed(HeroDbContext context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method
            //  to avoid creating duplicate seed data.

            //SeedDataPowers(context);
            //SeedDataHeroes(context);
            //SeedDataPowerDetails(context);
        }
示例#7
0
        public void CreateHero(HeroInfo heroInfo)
        {
            using (var context = new HeroDbContext(_nameOrConnectionString))
            {
                var hero = new Hero(heroInfo.Id, heroInfo.SerialNumber, heroInfo.Name, heroInfo.Power,
                    heroInfo.AlterEgo);

                context.Set<Hero>().Add(hero);

                context.SaveChanges();
            }
        }
示例#8
0
        private void SeedDataPowers(HeroDbContext context)
        {
            string[] powerNames = Enum.GetNames(typeof(EPower));
            if (powerNames != null && powerNames.Length > 0)
            {
                Power[] powers = new Power[powerNames.Length];
                for (int i = 0; i < powers.Length; i++)
                {
                    powers[i] = new Power(powerNames[i]);
                }

                context.Powers.AddOrUpdate(powers);
            }
        }
示例#9
0
        private void SeedDataHeroes(HeroDbContext context)
        {
            string[] heroNames = { "Thor", "Batman", "Hulk", "Doctor Strange", "Superman", "Venom", "Flash", "Spiderman", "Supergirl", "Deadpool" };
            if (heroNames != null && heroNames.Length > 0)
            {
                Hero[] heroes = new Hero[heroNames.Length];
                for (int i = 0; i < heroes.Length; i++)
                {
                    heroes[i] = new Hero(heroNames[i]);
                }

                context.Heroes.AddOrUpdate(heroes);
            }
        }
示例#10
0
        // POST: api/Heroes
        public async Task <IHttpActionResult> Post([FromBody] HeroCreate hero)
        {
            if (hero == null || string.IsNullOrEmpty(hero.HeroName) || string.IsNullOrWhiteSpace(hero.HeroName))
            {
                return(BadRequest(string.Format(GeneralMessages.InvalidData, ModelNames.Hero)));
            }

            using (HeroDbContext context = new HeroDbContext())
            {
                context.Heroes.Add(new Hero(hero));
                await context.SaveChangesAsync();

                return(Ok(new CMessage(string.Format(GeneralMessages.CreateSuccess, ModelNames.Hero))));
            }
        }
示例#11
0
        // GET: api/Heroes/5
        public async Task <IHttpActionResult> Get(int id)
        {
            using (HeroDbContext context = new HeroDbContext())
            {
                Hero hero = await context.Heroes.FirstOrDefaultAsync(h => h.HeroId == id);

                if (hero != null)
                {
                    HeroInfo heroInfo = new HeroInfo(hero);
                    return(Json(heroInfo));
                }
            }

            return(BadRequest(string.Format(GeneralMessages.NotFound, ModelNames.Hero, _heroMessages.GetString("HeroId"), id)));
        }
示例#12
0
        // DELETE: api/Heroes/5
        public async Task <IHttpActionResult> Delete(int id)
        {
            using (HeroDbContext context = new HeroDbContext())
            {
                Hero findHero = await context.Heroes.FirstOrDefaultAsync(h => h.HeroId == id);

                if (findHero == null)
                {
                    return(BadRequest(string.Format(GeneralMessages.NotFound, ModelNames.Hero, _heroMessages.GetString("HeroId"), id)));
                }

                context.Heroes.Remove(findHero);
                await context.SaveChangesAsync();

                return(Ok(new CMessage(string.Format(GeneralMessages.DeleteSuccess, ModelNames.Hero, _heroMessages.GetString("HeroId"), id))));
            }
        }
示例#13
0
        // GET: api/Heroes
        public async Task <IHttpActionResult> Get()
        {
            List <HeroInfo> result = new List <HeroInfo>();

            using (HeroDbContext context = new HeroDbContext())
            {
                var heroes = await context.Heroes.Where(h => true).ToListAsync();

                foreach (Hero hero in heroes)
                {
                    await context.Entry(hero).Collection(h => h.PowerDetails).LoadAsync();

                    result.Add(new HeroInfo(hero));
                }
            }

            return(Json(result));
        }
示例#14
0
        public async Task <ActionResult> InsertComment([FromBody] PostComment comment)
        {
            bool status = false;

            if (ModelState.IsValid)
            {
                try
                {
                    using (HeroDbContext db = new HeroDbContext())
                    {
                        string   IP        = Request.HttpContext.Connection.RemoteIpAddress.ToString();
                        DateTime date      = DateTime.Now;
                        int      heroCount = await db.Heroes.Where(h => h.HeroID == comment.HeroID).CountAsync();

                        if (heroCount > 0)
                        {
                            Comments cm = new Comments()
                            {
                                HeroID      = comment.HeroID,
                                Name        = comment.Name.Trim(),
                                Email       = comment.Email.Trim(),
                                Message     = comment.Message.Trim(),
                                IPAddress   = IP,
                                CommentDate = date
                            };

                            db.Comments.Add(cm);
                            int row = await db.SaveChangesAsync();

                            if (row > 0)
                            {
                                status = true;
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    return(BadRequest());
                }
            }
            return(Ok(status));
        }
示例#15
0
        public async Task <ActionResult> GetSearchHero(string heroid)
        {
            string heroDetail = "";

            try
            {
                using (HeroDbContext db = new HeroDbContext())
                {
                    var hero = await(from h in db.Heroes where h.HeroName.Contains(heroid)
                                     select new { h.HeroID, h.HeroName }).AsNoTracking().ToListAsync();

                    heroDetail = JsonConvert.SerializeObject(hero);
                }
            }
            catch (Exception)
            {
                return(BadRequest());
            }
            return(Ok(heroDetail));
        }
示例#16
0
        public ActionResult DeleteComment([FromBody] Account commentid)
        {
            bool status = false;

            try
            {
                using (HeroDbContext db = new HeroDbContext())
                {
                    /*var comments = await (from c in db.Comments where c.CommentID == commentid select c).FirstOrDefaultAsync();
                     * db.Comments.Remove(comments);
                     * var row = await db.SaveChangesAsync();
                     * if(row > 0) status = true;*/
                }
            }
            catch (Exception)
            {
                return(BadRequest());
            }
            return(Ok(status));
        }
示例#17
0
        public async Task <ActionResult> getHeroDetail(string heroid)
        {
            string heroDetail = "";

            try
            {
                using (HeroDbContext db = new HeroDbContext())
                {
                    var hero = await(from h in db.Heroes where h.HeroID == heroid
                                     join v in db.Votes on h.HeroID equals v.HeroID into result1
                                     from r1 in result1.DefaultIfEmpty() group r1 by new { h.HeroID, h.HeroName, h.HeroDesc, h.HeroIMG } into result2
                                     select new HeroDetail {
                        HeroID    = result2.Key.HeroID, HeroName = result2.Key.HeroName, HeroDesc = result2.Key.HeroDesc,
                        HeroIMG   = (result2.Key.HeroIMG != null && result2.Key.HeroIMG != "" ? result2.Key.HeroIMG : "noimages.jpg;"),
                        HeroVoted = result2.Count(c => c != null)
                    }).AsNoTracking().FirstOrDefaultAsync();

                    if (hero != null)
                    {
                        string[] HeroIMG = hero.HeroIMG.Split(";");
                        HeroIMG = HeroIMG.Take(HeroIMG.Count() - 1).ToArray(); //ไม่เอา array element ช่องสุดท้าย

                        hero.HeroIMG = "";
                        foreach (var h in HeroIMG)
                        {
                            hero.HeroIMG += imgStorage + h + ";";
                        }
                    }



                    heroDetail = JsonConvert.SerializeObject(hero);
                }
            }
            catch (Exception)
            {
                return(BadRequest());
            }
            return(Ok(heroDetail));
        }
示例#18
0
        private void SeedDataPowerDetails(HeroDbContext context)
        {
            PowerDetail[] powerDetails = new PowerDetail[16];
            powerDetails[0]  = new PowerDetail(1, (int)EPower.Thunder);
            powerDetails[1]  = new PowerDetail(1, (int)EPower.Strength);
            powerDetails[2]  = new PowerDetail(2, (int)EPower.Rich);
            powerDetails[3]  = new PowerDetail(2, (int)EPower.Strength);
            powerDetails[4]  = new PowerDetail(3, (int)EPower.Strength);
            powerDetails[5]  = new PowerDetail(4, (int)EPower.Magic);
            powerDetails[6]  = new PowerDetail(5, (int)EPower.Strength);
            powerDetails[7]  = new PowerDetail(5, (int)EPower.Speed);
            powerDetails[8]  = new PowerDetail(5, (int)EPower.LaserEyes);
            powerDetails[9]  = new PowerDetail(6, (int)EPower.Strength);
            powerDetails[10] = new PowerDetail(6, (int)EPower.Speed);
            powerDetails[11] = new PowerDetail(7, (int)EPower.Speed);
            powerDetails[12] = new PowerDetail(8, (int)EPower.Strength);
            powerDetails[13] = new PowerDetail(9, (int)EPower.Strength);
            powerDetails[14] = new PowerDetail(9, (int)EPower.LaserEyes);
            powerDetails[15] = new PowerDetail(10, (int)EPower.Strength);

            context.PowerDetails.AddOrUpdate(powerDetails);
        }
示例#19
0
        // PUT: api/Heroes/5
        public async Task <IHttpActionResult> Put(int id, [FromBody] HeroCreate hero)
        {
            if (hero == null || string.IsNullOrEmpty(hero.HeroName) || string.IsNullOrWhiteSpace(hero.HeroName))
            {
                return(BadRequest(string.Format(GeneralMessages.InvalidData, ModelNames.Hero)));
            }

            using (HeroDbContext context = new HeroDbContext())
            {
                Hero findHero = await context.Heroes.FirstOrDefaultAsync(h => h.HeroId == id);

                if (findHero == null)
                {
                    return(BadRequest(string.Format(GeneralMessages.NotFound, ModelNames.Hero, _heroMessages.GetString("HeroId"), id)));
                }

                findHero.HeroName = hero.HeroName;
                await context.SaveChangesAsync();

                return(Ok(new CMessage(string.Format(GeneralMessages.UpdateSuccess, ModelNames.Hero, _heroMessages.GetString("HeroId"), id))));
            }
        }
示例#20
0
        public async Task <ActionResult> GetHeroComment(string heroid)
        {
            string Comments = "";

            try
            {
                using (HeroDbContext db = new HeroDbContext())
                {
                    var hero = await(from c in db.Comments where c.HeroID == heroid
                                     select new { c.CommentID, CommentGroup = (c.ParentID != 0 ? c.ParentID : c.CommentID),
                                                  //c.Name, c.Email, c.Message, CommentDate = c.CommentDate.ToString("d MMMM yyyy เวลา HH.mm น.")})
                                                  c.Name, c.Email, c.Message, CommentDate = c.CommentDate })
                               .OrderBy(c => c.CommentGroup).ThenBy(c => c.CommentID).AsNoTracking().ToListAsync();

                    Comments = JsonConvert.SerializeObject(hero);
                }
            }
            catch (Exception)
            {
                return(BadRequest());
            }
            return(Ok(Comments));
        }
示例#21
0
        public async Task <ActionResult> getMoreHero(int skipNum)
        {
            string heroList = "";

            try
            {
                using (HeroDbContext db = new HeroDbContext())
                {
                    var hero = await(from h in db.Heroes
                                     let vCount = (from v in db.Votes where h.HeroID == v.HeroID select v).Count()
                                                  let cCount = (from c in db.Comments where h.HeroID == c.HeroID select c).Count()
                                                               select new { h.HeroID, h.HeroName,
                                                                            HeroIMG   = imgStorage + (h.HeroIMG != null && h.HeroIMG != "" ? h.HeroIMG : "noimages.jpg;"),
                                                                            HeroVoted = vCount, HeroCommented = cCount })
                               .OrderByDescending(h => h.HeroVoted).AsNoTracking().Skip(skipNum).Take(4).ToListAsync();
                    heroList = JsonConvert.SerializeObject(hero);
                }
            }
            catch (Exception)
            {
                return(BadRequest());
            }
            return(Ok(heroList));
        }
示例#22
0
 public PostRepository(HeroDbContext dbContext, IMapper mapper)
 {
     this.dbContext = dbContext;
     this.mapper    = mapper;
 }
示例#23
0
 public HeroRepository(HeroDbContext context) : base(context)
 {
 }
 public MetaHumansTeamsRepository(HeroDbContext context) : base(context)
 {
 }
示例#25
0
 public ProfileController(HeroDbContext context, IHelpers helper)
 {
     _context = context;
     _helper  = helper;
 }
示例#26
0
 public HeroController(HeroDbContext dbContext, IHeroApplicationService heroService)
 {
     this._dbContext   = dbContext;
     this._heroService = heroService;
 }
示例#27
0
 public TeamsController(HeroDbContext context, IMapper mapper) : base(mapper)
 {
     teamsRepository = new TeamsRepository(context);
 }
示例#28
0
 public AbilitiesRepository(HeroDbContext context) : base(context)
 {
 }
示例#29
0
 public HeroController(HeroDbContext context)
 {
     _context = context;
 }
示例#30
0
 public Repository(HeroDbContext dbContext)
 {
     Db    = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
     DbSet = Db.Set <TEntity>();
 }
示例#31
0
 public VillainsController(HeroDbContext context)
 {
     _context = context;
 }
示例#32
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MetaHumansAbilitiesController"/> class.
 /// </summary>
 /// <param name="context">The database context</param>
 public MetaHumansAbilitiesController(HeroDbContext context)
 {
     this.metahumanAbilitiesRepository = new MetaHumansAbilitiesRepository(context);
 }