예제 #1
0
        // GET: Samourais/Create
        public ActionResult Create()
        {
            SamuraiViewModel samouraiViewModel = new SamuraiViewModel();

            samouraiViewModel.Armes = db.Armes.ToList();
            return(View(samouraiViewModel));
        }
예제 #2
0
        // GET: Samurais/Create
        public ActionResult Create()
        {
            var armesUsedIds     = db.Samurais.Where(x => x.Arme != null).Select(x => x.Arme.Id).ToList();
            SamuraiViewModel svm = new SamuraiViewModel()
            {
                armes       = db.Armes.Where(a => !armesUsedIds.Contains(a.Id)).Select(a => a).ToList(),
                ArtMartials = db.ArtMartials.ToList()
            };

            return(View(svm));
        }
예제 #3
0
        public ActionResult Create(SamuraiViewModel samuraiViewModel)
        {
            if (ModelState.IsValid)
            {
                samuraiViewModel.Samurai.Arme = db.Armes.Find(samuraiViewModel.IdSelectedArme);
                db.Samourais.Add(samuraiViewModel.Samurai);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            samuraiViewModel.Armes = db.Armes.ToList();
            return(View(samuraiViewModel));
        }
예제 #4
0
        public ActionResult Create(SamuraiViewModel svm)
        {
            var armesUsedIds = db.Samurais.Where(x => x.Arme != null).Select(x => x.Arme.Id).ToList();

            svm.armes       = db.Armes.Where(a => !armesUsedIds.Contains(a.Id)).Select(a => a).ToList();
            svm.ArtMartials = db.ArtMartials.ToList();
            if (ModelState.IsValid && SamuraiValidator.Validate(svm, ModelState, db))
            {
                db.Samurais.Add(svm.Samurai);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            return(View(svm));
        }
예제 #5
0
 public ActionResult Edit(SamuraiViewModel samuraiViewModel)
 {
     if (ModelState.IsValid)
     {
         Samourai samourai = db.Samourais.Include(x => x.Arme).SingleOrDefault(x => x.Id == samuraiViewModel.Samurai.Id);
         samourai.Nom             = samuraiViewModel.Samurai.Nom;
         samourai.Force           = samuraiViewModel.Samurai.Force;
         samourai.Arme            = db.Armes.Find(samuraiViewModel.IdSelectedArme);
         db.Entry(samourai).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     samuraiViewModel.Armes = db.Armes.ToList();
     return(View(samuraiViewModel));
 }
예제 #6
0
        // GET: Samourais/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Samourai samourai = db.Samourais.Find(id);

            if (samourai == null)
            {
                return(HttpNotFound());
            }
            SamuraiViewModel samuraiViewModel = new SamuraiViewModel();

            samuraiViewModel.Armes          = db.Armes.ToList();
            samuraiViewModel.Samurai        = samourai;
            samuraiViewModel.IdSelectedArme = samourai.Arme?.Id;
            return(View(samuraiViewModel));
        }
예제 #7
0
        // GET: Samurais/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Samurai samurai = db.Samurais.Find(id);

            if (samurai == null)
            {
                return(HttpNotFound());
            }
            var armesUsedIds     = db.Samurais.Where(x => x.Arme != null && x.Id != id).Select(x => x.Arme.Id).ToList();
            SamuraiViewModel svm = new SamuraiViewModel()
            {
                armes          = db.Armes.Where(a => !armesUsedIds.Contains(a.Id)).Select(a => a).ToList(),
                ArtMartials    = db.ArtMartials.ToList(),
                Samurai        = samurai,
                armeId         = (samurai.Arme == null ? 0 : samurai.Arme.Id),
                ArtMartiauxIds = samurai.ArtMartials.Select(a => a.Id).ToList()
            };

            return(View(svm));
        }
예제 #8
0
        public static bool Validate(SamuraiViewModel svm, ModelStateDictionary ModelState, DojoContext context)
        {
            Samurai samurai = context.Samurais.Include(x => x.Arme).Include(x => x.ArtMartials).FirstOrDefault(s => s.Id == svm.Samurai.Id);

            if (context.Samurais.FirstOrDefault(s => s.Nom.Equals(svm.Samurai.Nom) && s.Id != svm.Samurai.Id) != null)
            {
                ModelState.AddModelError("Samurai.Nom", "Un samuraï portant se nom existe déjà.");
            }

            if (svm.Samurai.Force < 0 || svm.Samurai.Force > 1000000)
            {
                ModelState.AddModelError("Samurai.Force", "La force du samurai doit être entre 0 et 1 000 000.");
            }


            if (svm.armeId != null)
            {
                Arme    arme = context.Armes.FirstOrDefault(a => a.Id == svm.armeId);
                Samurai samuraiWithArme;
                if (samurai == null)
                {
                    samuraiWithArme = context.Samurais.Include(x => x.Arme).Where(s => s.Arme.Id.Equals(arme.Id)).FirstOrDefault();
                }
                else
                {
                    samuraiWithArme = context.Samurais.Include(x => x.Arme).Where(s => s.Arme.Id.Equals(arme.Id) && s.Id != samurai.Id).FirstOrDefault();
                }
                if (arme == null)
                {
                    ModelState.AddModelError("armeId", "L'arme que vous avez indiqué n'existe pas.");
                }
                else if (samuraiWithArme != null)
                {
                    if (samurai == null)
                    {
                        ModelState.AddModelError("armeId", "L'arme que vous avez indiqué est déjà utilisée.");
                    }
                }
                else
                {
                    if (svm.Samurai.Id != 0)
                    {
                        samurai.Nom   = svm.Samurai.Nom;
                        samurai.Force = svm.Samurai.Force;
                        svm.Samurai   = samurai;
                    }
                    svm.Samurai.Arme = arme;
                }
            }
            else
            {
                if (samurai != null)
                {
                    svm.Samurai = samurai;
                }
                svm.Samurai.Arme = null;
            }

            if (svm.ArtMartiauxIds != null)
            {
                svm.Samurai.ArtMartials = svm.ArtMartials.Where(a => svm.ArtMartiauxIds.Contains(a.Id)).Select(a => a).ToList();
            }
            else
            {
                svm.Samurai.ArtMartials = null;
            }


            if (ModelState.IsValid)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }