Exemplo n.º 1
0
        public Ninja UpdateEquipments(Ninja ninja, List <int> equipments)
        {
            using var ctx = new NinjaManagerContext();

            ctx.Attach(ninja);

            ctx.Entry(ninja).Collection(n => n.Equipments).Load();

            var equipmentsInNinja = ninja.Equipments.Select(i => i.EquipmentId).ToArray();

            foreach (var equipment in ctx.Equipments)
            {
                if (equipments.Contains(equipment.Id))
                {
                    if (!equipmentsInNinja.Contains(equipment.Id))
                    {
                        ninja.Equipments.Add(new NinjaEquipment {
                            NinjaId = ninja.Id, EquipmentId = equipment.Id
                        });
                    }
                }
                else if (equipmentsInNinja.Contains(equipment.Id))
                {
                    ctx.Remove(ninja.Equipments.FirstOrDefault(ne => ne.EquipmentId == equipment.Id) !);
                }
            }

            ctx.Ninjas.Update(ninja);
            ctx.SaveChanges();
            return(ninja);
        }
Exemplo n.º 2
0
 public Ninja Create(Ninja ninja)
 {
     using var ctx = new NinjaManagerContext();
     ctx.Ninjas.Add(ninja);
     ctx.SaveChanges();
     return(ninja);
 }
Exemplo n.º 3
0
 public Equipment Create(Equipment equipment)
 {
     using var ctx = new NinjaManagerContext();
     ctx.Equipments.Add(equipment);
     ctx.SaveChanges();
     return(equipment);
 }
Exemplo n.º 4
0
        public bool Delete(int id)
        {
            using var ctx = new NinjaManagerContext();
            var equipment = ctx.Equipments.Find(id);

            if (equipment == null)
            {
                return(false);
            }

            var ninjas = GetNinjaFromEquipments(equipment);

            var repo = new NinjaRepository();

            foreach (var ninja in ninjas.ToList())
            {
                ninja.Gold += equipment.Cost;

                var newEquipments = repo.GetEquipmentsFromNinja(ninja).Select(e => e.Id).ToList();
                newEquipments.Remove(equipment.Id);

                repo.UpdateEquipments(ninja, newEquipments);

                repo.Update(ninja);
            }

            ctx.Equipments.Remove(equipment);
            ctx.SaveChanges();

            return(true);
        }
 public NinjaEquipment Create(NinjaEquipment ninjaEquipment)
 {
     using var ctx = new NinjaManagerContext();
     ctx.NinjaEquipments.Add(ninjaEquipment);
     ctx.SaveChanges();
     return(ninjaEquipment);
 }
Exemplo n.º 6
0
 public Ninja Update(Ninja ninja)
 {
     using var ctx = new NinjaManagerContext();
     ctx.Attach(ninja);
     ctx.Ninjas.Update(ninja);
     ctx.SaveChanges();
     return(ninja);
 }
Exemplo n.º 7
0
 public Equipment Update(Equipment equipment)
 {
     using var ctx = new NinjaManagerContext();
     ctx.Attach(equipment);
     ctx.Equipments.Update(equipment);
     ctx.SaveChanges();
     return(equipment);
 }
Exemplo n.º 8
0
        public bool Delete(int id)
        {
            using var ctx = new NinjaManagerContext();
            var toRemove = ctx.Ninjas.Find(id);

            if (toRemove == null)
            {
                return(false);
            }

            ctx.Ninjas.Remove(toRemove);
            ctx.SaveChanges();

            return(true);
        }
        public bool Delete(int ninjaId, int equipmentId)
        {
            using var ctx = new NinjaManagerContext();
            var toRemove = ctx.NinjaEquipments.First(o => o.EquipmentId == equipmentId && o.NinjaId == ninjaId);

            if (toRemove == null)
            {
                return(false);
            }

            ctx.NinjaEquipments.Remove(toRemove);
            ctx.SaveChanges();

            return(true);
        }