示例#1
0
        public Result <Candidate> Handler(UpdateCandidateCommand cmd)
        {
            var candidate           = cmd.Candidate;
            var _candidateValidator = new CandidateValidator();

            if (!_candidateValidator.IsValid(candidate))
            {
                var brokerRules = _candidateValidator.BrokenRules(candidate);

                return(Result <Candidate> .Fail <Candidate>(CoreHelper.MergeErrors(brokerRules)));
            }
            try
            {
                var entity = _context.Candidates.Find(cmd.Candidate.Id);
                entity.Name = cmd.Candidate.Name;

                _context.SaveChanges();

                return(Result <Candidate> .OK <Candidate>(entity));
            }
            catch (Exception ex)
            {
                return(Result <Candidate> .Fail <Candidate>(ex.Message));
            }
        }
示例#2
0
        public Result <Candidate> Handler(CreateCandidateCommand cmd)
        {
            var candidate           = cmd.Candidate;
            var _candidateValidator = new CandidateValidator();

            if (!_candidateValidator.IsValid(candidate))
            {
                var brokerRules = _candidateValidator.BrokenRules(candidate);
                return(Result.Fail <Candidate>(CoreHelper.MergeErrors(brokerRules)));
            }
            //Try to insert
            try
            {
                _context.Candidates.Add(candidate);
                _context.SaveChanges();
                return(Result.OK <Candidate>(candidate));
            }
            catch (Exception ex)
            {
                return(Result <Candidate> .Fail <Candidate>(ex.Message));
            }
        }