private choiceStr Edit(choiceStr c)
        {
            choiceOdd co = db.choiceOdds.Where(p => p.choiceSn == c.choiceSn).FirstOrDefault();

            //co.Odds = c.Odds;
            db.SaveChanges();
            return(c);
        }
 public choiceStr setData(choiceStr c)
 {
     if (db.choiceStrs.Where(p => p.choiceSn == c.choiceSn).Count() == 0)
     {
         return(AddNew(c));
     }
     else
     {
         return(Edit(c));
     }
 }
 private choiceStr AddNew(choiceStr c)
 {
     db.choiceStrs.Add(c);
     db.SaveChanges();
     return(c);
 }
        public IHttpActionResult Postgame(gameDto gameD)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            user u = db.users.Where(p => p.userID == gameD.userId && p.comSn == gameD.comSn).FirstOrDefault();

            if (u == null)
            {
                return(BadRequest("UserID不存在"));
            }
            else
            {
                gameD.userSn = u.sn;
            }

            Mapper.Initialize(cfg => {
                cfg.CreateMap <gameDto, game>();
                cfg.CreateMap <topicDto, topic>();
                cfg.CreateMap <choiceDto, choice>();
                cfg.CreateMap <choiceStrDto, choiceStr>();
                cfg.CreateMap <bonusDto, bonu> ();
            });
            game game = Mapper.Map <game>(gameD);

            game.gameStatus = 1;      //遊戲建立的時候是關閉狀態
            game.valid      = 1;
            //game.createDate = DateTime.Now;
            db.games.Add(game);
            db.SaveChanges();
            foreach (topicDto tD in gameD.topicList)
            {
                if (tD.valid.HasValue && tD.valid.Value == 1)
                {
                    topic t = Mapper.Map <topic>(tD);
                    t.gameSn = game.sn;
                    t.comSn  = game.comSn;
                    if (!t.sdate.HasValue)
                    {
                        t.sdate = gameD.sdate;
                    }
                    if (!t.edate.HasValue)
                    {
                        t.edate = gameD.edate;
                    }
                    db.topics.Add(t);
                    db.SaveChanges();
                    foreach (choiceDto cD in tD.choiceList)
                    {
                        if (cD.valid.HasValue && cD.valid.Value == 1)
                        {
                            choice c = Mapper.Map <choice>(cD);
                            c.comSn   = game.comSn;
                            c.topicSn = t.sn;
                            db.choices.Add(c);
                            db.SaveChanges();
                            //龍的傳人才會新增
                            if (game.betModel == 5)
                            {
                                foreach (choiceStrDto cb in cD.choiceString)
                                {
                                    choiceStr cs = Mapper.Map <choiceStr>(cb);
                                    //只有在可下注單位的資料要存
                                    //if (game.betUnit.Contains(cs.unitSn.ToString()))
                                    //{
                                    cs.choiceSn    = c.sn;
                                    cs.choiceStr1  = cb.choiceStr;
                                    cs.eChoiceStr1 = cb.eChoiceStr;
                                    cs.unitSn      = 1;
                                    db.choiceStrs.Add(cs);
                                    db.SaveChanges();
                                    //cs = new choiceStrRepository().setData(cs);
                                    //}
                                }
                            }
                        }
                    }
                }
            }
            //獎金置入
            if (gameD.bonusList != null)
            {
                foreach (bonusDto bod in gameD.bonusList)
                {
                    bonu b = Mapper.Map <bonu>(bod);
                    b.gamesn = game.sn;
                    db.bonus.Add(b);
                    db.SaveChanges();
                }
            }



            return(CreatedAtRoute("DefaultApi", new { id = game.sn }, game));
        }