Ejemplo n.º 1
0
        // GET: Groups
        public async Task <ActionResult> Index()
        {
            var data = await groupsRepository.GetAllAsync();

            var model = data.Select(x => new GroupModel
            {
                Id   = x.Id,
                Name = x.Name,
                Year = x.Year
            });

            return(View(model));
        }
        public AddStudentToGroupValidator(IUsersRepository users, IUserContext user, IGroupsRepository groups)
        {
            _usersRepository = users;
            RuleFor(x => x.StudentName)
            .NotEmpty().WithMessage(string.Format(Consts.FieldEmptyMessage, "Student Name"))
            .MaximumLength(Consts.MaxLength)
            .WithMessage(string.Format(Consts.FieldMaximumLengthMessage, "Student Name", Consts.MaxLength));

            RuleFor(x => x).Custom((x, context) =>
            {
                var student = _usersRepository.GetStudentsByNamePrefixAsync(x.StudentName).Result;
                if (student.Count == 0)
                {
                    context.AddFailure("StudentName", "Student name is not valid");
                    return;
                }
                var group = groups.GetByIdAsync(x.GroupId).Result;
                if (null == group)
                {
                    context.AddFailure("", "Invalid group id");
                    return;
                }
                foreach (var userGroup in group.UserGroups)
                {
                    if (userGroup.User.UserName.Equals(x.StudentName))
                    {
                        context.AddFailure("StudentName", "This user is already in this group");
                        return;
                    }
                }
            });
            RuleFor(x => x.GroupId)
            .NotEmpty().WithMessage(string.Format(Consts.FieldEmptyMessage, "Group Id"))
            .Custom((x, context) =>
            {
                var groupsList = groups.GetAllAsync().Result.Where(a => a.Id == x).ToList();

                if (groupsList.Count == 0)
                {
                    context.AddFailure("Group Id", "Group Id is not valid");
                }
                else
                {
                    if (user.GetLogedInUserId() != groupsList[0].UserId)
                    {
                        context.AddFailure("Group Id", "Unauthorized");
                    }
                }
            });
        }
Ejemplo n.º 3
0
        public async Task <IList <GroupModel> > GetAllAsync(string requestId = "")
        {
            _logger.LogInformation($"RequestId: {requestId} - GetAllAsync called.");

            try
            {
                var modelListItems = (await _groupsRepository.GetAllAsync(requestId)).Select(item => MapToModel(item)).ToList();

                if (modelListItems.Count == 0)
                {
                    _logger.LogWarning($"RequestId: {requestId} - GetAllAsync no items found");
                    throw new NoItemsFound($"RequestId: {requestId} - Method name: GetAllAsync - No Items Found");
                }

                return(modelListItems);
            }
            catch (Exception ex)
            {
                _logger.LogError($"RequestId: {requestId} - GetAllAsync Service Exception: {ex}");
                throw new ResponseException($"RequestId: {requestId} - GetAllAsync Service Exception: {ex}");
            }
        }