Example #1
0
        private (List <TagVM> newtags, List <TagVM> oldtags) _InsertTags(SoalVM soalVM)
        {
            var q = new List <TagVM>();

            var e = new List <TagVM>();

            foreach (var n in soalVM.Tags.Where(x => x.Id == 0))
            {
                q.Add(new TagVM
                {
                    Onvan = n.name,
                    Id    = n.Id,
                }
                      );
            }
            q = _tagRepository.CreatRange(q).ToList();

            foreach (var n in soalVM.Tags.Where(x => x.Id != 0))
            {
                e.Add(new TagVM
                {
                    Onvan = n.name,
                    Id    = n.Id,
                }
                      );
            }

            //var s = q.Union(e);
            return(q.ToList(), e.ToList());
        }
Example #2
0
        public IActionResult Put(long id, [FromBody] SoalVM soal)
        {
            var us = _users.GetCurrentUserId();

            if (
                !ModelState.IsValid ||
                !_soal.isSoalOfuser(soal.Id, us) ||
                (soal.Id != id) ||
                (soal == null || id < 1))
            {
                return(BadRequest());
            }
            if (!soal.Tags.Any())
            {
                soal.Tags.Add(new JsonVm {
                    Id = 1
                });
            }
            var q = _soal.newEditforSoal(soal);

            if (q)
            {
                return(Ok());
            }


            return(BadRequest());
        }
Example #3
0
 public IActionResult Create([FromBody] SoalVM soal)
 {
     // if (!ModelState.IsValid
     // || (soal == null))
     // {
     //     return BadRequest("**  شیطون بلا  **");
     // }
     // var q = _soal.postforSoal(soal, 5);
     // if (q)
     // {
     //     return Ok(new JsonResult("*** علی باوامی ***"));
     // }
     return(StatusCode(500));
 }
Example #4
0
        public IActionResult Create([FromBody] SoalVM soal)
        {
            if (!ModelState.IsValid ||
                (soal == null))
            {
                return(BadRequest("**  شیطون بلا  **"));
            }
            var q = _soal.postforSoal(soal, _users.GetCurrentUserId());

            if (q)
            {
                return(Ok(new JsonResult("*** علی باوامی ***")));
            }
            return(StatusCode(500));
        }
Example #5
0
 public SoalVM getSoalForEdit(long id)
 {
     try
     {
         var soal = _soalRepository.GetById(id);
         var zr   = soal.TagSoal.Where(x => !x.Isdeleted).FirstOrDefault().Tag;
         var s    = new SoalVM
         {
             Id   = soal.Id,
             Matn = soal.Matn,
         };
         return(s);
     }
     catch { return(null); }
 }
Example #6
0
        public bool postforSoal(SoalVM soalVM, long UserId)
        {
            try
            {
                var q = new List <TagVM>();

                var e = new List <TagVM>();

                foreach (var n in soalVM.Tags.Where(x => x.Id == 0))
                {
                    q.Add(new TagVM
                    {
                        Onvan        = n.name,
                        Id           = n.Id,
                        ZirReshtehId = soalVM.ZirReshtehId
                    }
                          );
                }
                q = _tagRepository.CreatRange(q).ToList();

                foreach (var n in soalVM.Tags.Where(x => x.Id != 0))
                {
                    e.Add(new TagVM
                    {
                        Onvan        = n.name,
                        Id           = n.Id,
                        ZirReshtehId = soalVM.ZirReshtehId
                    }
                          );
                }

                var s = q.Union(e);
                var w = s.Select(c => c.Id).ToArray();

                // q.AddRange(soalVM.Tags.Where(x => x.Id == 0)
                // .Select(c => new TagVM { ZirReshtehId = c.Id }));
                _soalRepository.Insert(new Soal
                {
                    Matn = soalVM.Matn,
                    ApplicationUserId = UserId,
                    ZirReshtehId      = soalVM.ZirReshtehId
                },
                                       w);
                db.SaveAllChanges();
                return(true);
            }
            catch (Exception e) { throw e; }
        }
Example #7
0
        public bool newEditforSoal(SoalVM soalVM)
        {
            try
            {
                var sl = _soalRepository.GetById(soalVM.Id);

                sl.Matn = soalVM.Matn;
                if (soalVM.Tags != null)
                {
                    var(nw, old) = _InsertTags(soalVM);
                    var d = old.Select(f => f.Id);

                    var delettags = db.Set <TagSoal>().Where(c => c.Soal == sl).ToList();
                    // delettags.ForEach(v=> v.Isdeleted = true
                    // );
                    db.Set <TagSoal>().RemoveRange(delettags);
                    db.SaveAllChanges();
                    var s = old.Union(nw);
                    var w = s.Select(x => x.Id).ToArray();
                    foreach (var n in w)
                    {
                        db.Addnew <TagSoal>(new TagSoal {
                            Isdeleted = false, TagId = n, Soal = sl
                        });
                    }
                }
                else
                {
                    var tg = db.Set <TagSoal>().Where(x => x.Soal.Id.Equals(soalVM.Id)).ToList();
                    tg.ForEach(x => x.Isdeleted = true);
                }
                db.MarkAsChanged <Soal>(sl);

                db.SaveAllChanges();
                return(true);
            }
            catch
            {
                return(false);
            }
        }
Example #8
0
        public bool postforSoal(SoalVM soalVM, long UserId)
        {
            try
            {
                var(nw, old) = _InsertTags(soalVM);
                var s = old.Union(nw);

                var w = s.Select(c => c.Id).ToArray();

                _soalRepository.Insert(new Soal
                {
                    Matn = soalVM.Matn,
                    ApplicationUserId = UserId,
                    //ZirReshtehId = 1
                },
                                       w);
                db.SaveAllChanges();
                return(true);
            }
            catch (Exception e) { throw e; }
        }