Ejemplo n.º 1
0
        public async Task <IActionResult> Add(PersonnelVM vm)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var obj = (await objRep.Add(vm));

                    if (obj.Successed)
                    {
                        if (obj.ResultObject.PersonnelId > 0)
                        {
                            return(Ok(obj));
                        }
                        else
                        {
                            return(NotFound());
                        }
                    }
                    else
                    {
                        return(NotFound(obj));
                    }
                }
                catch (Exception)
                {
                    return(BadRequest());
                }
            }

            return(BadRequest());
        }
Ejemplo n.º 2
0
        public async Task <RepResult <PersonnelVM> > Login(string nationalCode, string passWord)
        {
            if (dbContext != null)
            {
                var query = from prs in dbContext.Personnels join user in dbContext.Users on prs.PersonnelId equals user.PersonnelId
                            where prs.NationalCode == nationalCode && user.Password == passWord select prs;


                if (await query.AnyAsync())
                {
                    var obj = await query.Include(c => c.Users).SingleOrDefaultAsync();

                    PersonnelRepository.LoadUser(obj);
                    return(new RepResult <PersonnelVM> {
                        Successed = true, ResultObject = PersonnelVM.GetVM(obj)
                    });
                }
                else
                {
                    return(new RepResult <PersonnelVM> {
                        Successed = false,
                        Message = "پرسنل با این کد ملی و پسورد در سیستم موجود نمی باشد"
                    });
                }
            }

            return(new RepResult <PersonnelVM>());
        }
        public async Task <RepResult <PersonnelVM> > Update(PersonnelVM vm)
        {
            if (dbContext != null)
            {
                if (!await dbContext.Personnels.AnyAsync(a => a.NationalCode.Contains(vm.NationalCode) && a.PersonnelId != vm.PersonnelId))
                {
                    var obj = PersonnelVM.GetAnyObject(vm, false);

                    dbContext.Entry(obj.Item1).State = EntityState.Modified;
                    dbContext.Entry(obj.Item2).State = EntityState.Modified;

                    await dbContext.SaveChangesAsync();

                    return(new RepResult <PersonnelVM> {
                        Successed = true, ResultObject = (await Get(obj.Item1.PersonnelId)).ResultObject
                    });
                }
                else
                {
                    return(new RepResult <PersonnelVM> {
                        Successed = false, Message = "پرسنل با این کد ملی در سیستم ثبت شده است. شما قادر به ثبت مجدد نمی باشید", ResultObject = null
                    });
                }
            }

            return(new RepResult <PersonnelVM>());
        }
        public async Task <RepResult <PersonnelVM> > Get(int id)
        {
            if (dbContext != null)
            {
                var obj = await entity.Include(c => c.Users).SingleOrDefaultAsync(s => s.PersonnelId == id);

                LoadUser(obj);

                return(new RepResult <PersonnelVM> {
                    Successed = true, ResultObject = PersonnelVM.GetVM(obj)
                });
            }

            return(new RepResult <PersonnelVM>());
        }
        public async Task <RepResult <IEnumerable <PersonnelVM> > > Get()
        {
            if (dbContext != null)
            {
                var objs = await entity.Include(c => c.Users).ToAsyncEnumerable().ToList();

                foreach (Personnel obj in objs)
                {
                    LoadUser(obj);
                }

                return(new RepResult <IEnumerable <PersonnelVM> > {
                    Successed = true, ResultObject = objs.Select(m => PersonnelVM.GetVM(m))
                });
            }

            return(new RepResult <IEnumerable <PersonnelVM> >());
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> Update(PersonnelVM vm)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var obj = (await objRep.Update(vm));

                    if (obj.Successed)
                    {
                        if (obj.ResultObject.PersonnelId > 0)
                        {
                            return(Ok(obj));
                        }
                        else
                        {
                            return(NotFound());
                        }
                    }
                    else
                    {
                        return(NotFound(obj));
                    }
                }
                catch (Exception ex)
                {
                    if (ex.GetType().FullName == "Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException")
                    {
                        return(NotFound());
                    }

                    return(BadRequest());
                }
            }

            return(BadRequest());
        }