Beispiel #1
0
        public static async Task <int> Handle(CreatePersonaCommand request, ITestNetDbContext _context, CancellationToken cancellationToken)
        {
            if (request.Cedula != "" && _context.Personas.Where(p => p.Cedula == request.Cedula).Any())
            {
                throw new PersonaExistsException(request.Nombre, request.Cedula);
            }
            if (request.EmpleoId > 0 && !_context.Empleos.Where(e => e.Id == request.EmpleoId).Any())
            {
                throw new NotFoundException("Empleo", request.EmpleoId);
            }

            var entity = new Persona {
                Nombre    = request.Nombre,
                Apellido  = request.Apellido,
                Cedula    = request.Cedula,
                Direccion = request.Direccion,
                EmpleoId  = request.EmpleoId
            };

            await _context.Personas.AddAsync(entity);

            await _context.SaveChangesAsync(cancellationToken);

            return(entity.Id);
        }
        public static async Task <PersonaDTO> Handle(GetPersonaQuery request, ITestNetDbContext _context)
        {
            var entity = await _context.Personas
                         .Include(p => p.Empleo)
                         .Where(p => p.Id == request.PersonaId)
                         .SingleOrDefaultAsync();

            if (entity == null)
            {
                throw new NotFoundException("Persona", request.PersonaId);
            }

            return(PersonaDTO.Create(entity));
        }
Beispiel #3
0
        public static async Task <List <PersonaDTO> > Handle(ITestNetDbContext _context)
        {
            var entities = await _context.Personas
                           .Include(p => p.Empleo)
                           .ToListAsync();

            var personasDto = entities.Select(p => PersonaDTO.Create(p)).ToList();

            /*
             * List<PersonaDTO> personasDto = new List<PersonaDTO>();
             *
             * foreach (var p in entities)
             * {
             *  personasDto.Add(PersonaDTO.Create(p));
             * }
             */
            return(personasDto);
        }
Beispiel #4
0
        public static async Task <int> Handle(UpdatePersonaCommand request, ITestNetDbContext _context, CancellationToken cancellationToken)
        {
            var entity = await _context.Personas.FindAsync(request.Id);

            if (entity == null)
            {
                throw new NotFoundException("Persona", request.Id);
            }
            if (request.EmpleoId > 0 && !_context.Empleos.Where(e => e.Id == request.EmpleoId).Any())
            {
                throw new NotFoundException("Empleo", request.EmpleoId);
            }

            entity.Nombre    = request.Nombre;
            entity.Apellido  = request.Apellido;
            entity.Direccion = request.Direccion;
            entity.EmpleoId  = request.EmpleoId;

            await _context.SaveChangesAsync(cancellationToken);

            return(request.Id);
        }
Beispiel #5
0
 public PersonasController(ITestNetDbContext context)
 {
     _context = context;
 }