public async Task <Response> UpsertActionRegister(UpsertActionRegisterCmd request)
        {
            if (!string.IsNullOrEmpty(request.OffishId))
            {
                var item = await _context.Offishes.SingleOrDefaultAsync(z => z.Id == request.OffishId);

                if (item == null)
                {
                    throw new BusinessLogicException("رکوردی یافت نشد");
                }
                item.PGMId      = request.PgmId;
                item.CategoryId = request.CategoryId;
                item.StartDate  = request.StartDate;
                item.StartTime  = request.StartTime;
                item.EndDate    = request.EndDate;
                item.EndTime    = request.EndTime;
                //avamel
                var offishusers = _context.OffishUsers.Where(x => x.OffishId == request.OffishId);
                _context.OffishUsers.RemoveRange(offishusers);
                foreach (var avamel in request.Avamel)
                {
                    var employee = new OffishUser
                    {
                        Id       = Guid.NewGuid().ToString(),
                        UserId   = avamel.UserId,
                        RoleId   = avamel.RoleId,
                        OffishId = request.OffishId
                    };
                    await _context.OffishUsers.AddAsync(employee);
                }

                ///
                _context.Offishes.Update(item);
            }
            else
            {
                var item = new Offish
                {
                    Id         = Guid.NewGuid().ToString(),
                    CategoryId = request.CategoryId,
                    StartDate  = request.StartDate,
                    StartTime  = request.StartTime,
                    EndDate    = request.EndDate,
                    EndTime    = request.EndTime
                };
                await _context.Offishes.AddAsync(item);


                //avamel
                var offishusers = _context.OffishUsers.Where(x => x.OffishId == request.OffishId);
                _context.OffishUsers.RemoveRange(offishusers);
                foreach (var avamel in request.Avamel)
                {
                    var employee = new OffishUser
                    {
                        Id       = Guid.NewGuid().ToString(),
                        UserId   = avamel.UserId,
                        RoleId   = avamel.RoleId,
                        OffishId = request.OffishId
                    };
                    await _context.OffishUsers.AddAsync(employee);
                }
            }

            await _context.SaveChangesAsync();

            return(new Response
            {
                Status = true,
                Message = "success"
            });
        }
Exemple #2
0
        public async Task <Response> UpsertActionRegister(UpsertActionRegisterCmd request)
        {
            var pgm = await _context.PGMs.SingleOrDefaultAsync(x => x.Id == request.PgmId);

            if (pgm == null)
            {
                throw new BusinessLogicException("چنین برنامه ای تعریف نشده است ");
            }

            var category = await _context.OffishCategories.SingleOrDefaultAsync(x => x.Id == request.CategoryId);

            if (category == null)
            {
                throw new BusinessLogicException("چنین گروهی تعریف نشده است ");
            }

            if (!string.IsNullOrEmpty(request.Id))
            {
                var item = await _context.Offishes.SingleOrDefaultAsync(z => z.Id == request.Id);

                if (item == null)
                {
                    throw new BusinessLogicException("رکوردی یافت نشد");
                }
                //=> define ofish code
                item.PGM       = pgm;
                item.Category  = category;
                item.StartDate = request.StartDate;
                item.StartTime = request.StartTime;
                item.EndDate   = request.EndDate;
                item.EndTime   = request.EndTime;

                //avamel first delete avamel then add new offish users
                var existAvamel = _context.OffishUsers.Where(x => x.OffishId == request.Id).AsQueryable();
                if (existAvamel != null)
                {
                    _context.OffishUsers.RemoveRange(existAvamel);
                    await _context.SaveChangesAsync();
                }
                foreach (var avamel in request.Avamel)
                {
                    var existUser = await _userManager.FindByIdAsync(avamel.UserId);

                    if (existUser == null)
                    {
                        throw new BusinessLogicException("چنین عواملی یافت نشد ");
                    }

                    var existRole = await _roleManager.FindByIdAsync(avamel.RoleId);

                    if (existRole == null)
                    {
                        throw new BusinessLogicException("چنین رولی یافت نشد ");
                    }


                    var employee = new OffishUser();
                    employee.Id        = Guid.NewGuid().ToString();
                    employee.User      = existUser;
                    employee.Role      = existRole;
                    employee.CreatedAt = DateTime.Now;
                    employee.OffishId  = item.Id;
                    await _context.OffishUsers.AddAsync(employee);
                }
                //avamel
                ///
                _context.Offishes.Update(item);
            }
            else
            {
                //creat


                var item = new Offish();

                item.Id        = Guid.NewGuid().ToString();
                item.PGM       = pgm;
                item.Category  = category;
                item.StartDate = request.StartDate;
                item.StartTime = request.StartTime;
                item.EndDate   = request.EndDate;
                item.EndTime   = request.EndTime;
                //avamel
                foreach (var avamel in request.Avamel)
                {
                    var existUser = await _userManager.FindByIdAsync(avamel.UserId);

                    if (existUser == null)
                    {
                        throw new BusinessLogicException("چنین عواملی یافت نشد ");
                    }

                    var existRole = await _roleManager.FindByIdAsync(avamel.RoleId);

                    if (existRole == null)
                    {
                        throw new BusinessLogicException("چنین رولی یافت نشد ");
                    }


                    var employee = new OffishUser();
                    employee.Id        = Guid.NewGuid().ToString();
                    employee.User      = existUser;
                    employee.Role      = existRole;
                    employee.CreatedAt = DateTime.Now;
                    employee.OffishId  = item.Id;
                    await _context.OffishUsers.AddAsync(employee);
                }
                //avamel
                await _context.Offishes.AddAsync(item);
            }

            await _context.SaveChangesAsync();

            return(new Response
            {
                Status = true,
                Message = "success"
            });
        }