Exemplo n.º 1
0
        public async Task <Result <UnitResult, Error> > Handle(AddNewExampleCommand request,
                                                               CancellationToken cancellationToken)
        {
            var student = CurrentContextProvider.CurrentUser() as Student;

            if (student == null)
            {
                return(Result.Failure <UnitResult, Error>(Error.AuthorizationError("The current user is not a student")));
            }

            if (request.Text.IsNullOrEmpty())
            {
                return(Result.Failure <UnitResult, Error>(
                           Error.ValidationError("The text has an invalid value for the example")));
            }

            var result = HskLevel.FromText(request.HskLevel);

            if (result.IsFailure)
            {
                return(Result.Failure <UnitResult, Error>(Error.ValidationError(result.Error)));
            }


            await ExampleRepository.AddAsync(new Example(request.Text, HskLevel.FromText(request.HskLevel).Value,
                                                         student));

            return(new UnitResult());
        }