示例#1
0
        public async Task <bool> UserEquipmentAssignAsync(User user, UserEquipmentAssignFormModel userEquipmentAssignForm)
        {
            try
            {
                var userEquipment = await _context.Users.Include(c => c.EquipmentUserSubscriptions).FirstOrDefaultAsync(c => c.Id == userEquipmentAssignForm.UserId);

                if (userEquipment == null)
                {
                    throw new ExperienceManagementGlobalException(UsersServiceErrors.UserNotFoundError);
                }
                var notExistEquipment = _context.Equipments.Any(c => !userEquipmentAssignForm.EquipmentIds.Contains(c.Id));
                if (notExistEquipment == false)
                {
                    throw new ExperienceManagementGlobalException(UsersServiceErrors.EquipmentNotFoundError);
                }

                var EqUserSubScriptions = userEquipment.EquipmentUserSubscriptions.ToList();
                _context.EquipmentUserSubscriptions.RemoveRange(EqUserSubScriptions);
                await _context.SaveChangesAsync();

                var equipments = await _context.Equipments.Where(c => userEquipmentAssignForm.EquipmentIds.Contains(c.Id)).ToListAsync();

                equipments.ForEach(async eq =>
                {
                    var userEqSubscription = new EquipmentUserSubscription
                    {
                        UserId    = userEquipment.Id,
                        Equipment = eq
                    };
                    await _context.EquipmentUserSubscriptions.AddAsync(userEqSubscription);
                });

                await _context.SaveChangesAsync();

                return(true);
            }
            catch (Exception ex)
            {
                throw new ExperienceManagementGlobalException(UsersServiceErrors.AssignmentError, ex);
            }
        }
        public async Task <IActionResult> UserEquipmentAssign([FromBody]  UserEquipmentAssignFormModel userEquipmentAssignForm)
        {
            var userEquipmentAssign = await _usersService.UserEquipmentAssignAsync(HttpContext.GetUser(), userEquipmentAssignForm);

            return(Ok(GetRequestResult(userEquipmentAssign)));
        }