Exemple #1
0
        public IHttpActionResult PutAlumno(int id, Alumno alumno)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != alumno.Id)
            {
                return(BadRequest());
            }

            db.Entry(alumno).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AlumnoExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Exemple #2
0
        public IHttpActionResult PutAlumno(int id, Alumno alumno)   // en put se envia alumno y id, en post solo el objeto
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != alumno.Id)
            {
                return(BadRequest());  //validaciones http
            }

            db.Entry(alumno).State = EntityState.Modified;  // se tiene q marcar el objeto como sucio, que se ha modificado

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)  // se tiene que hacer catch
            {
                if (!AlumnoExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent)); // NoContent devuelve VOID = todo ha ido bien
        }
Exemple #3
0
        public AlumnoEntity Add(AlumnoEntity alumnoEntity)
        {
            AlumnoEntity alumnoEntityAdded;

            // Aqui se haria el automapper para pasar a objeto
            try
            {
                Alumno alumnoDM = new Alumno(alumnoEntity.Nombre, alumnoEntity.Apellidos, alumnoEntity.Dni, alumnoEntity.FechaNacimiento, alumnoEntity.Edad);

                db.Alumno.Add(alumnoDM);
                db.SaveChanges();

                alumnoEntityAdded = MapperInfrastructureAlumno.AlumnoDataModelToAlumnoEntity(alumnoDM);
            }
            catch (DbUpdateConcurrencyException ex)
            {
                // YOU MUST LOG - Habria que hacer un log
                throw new VuelingExceptions(ResourceMessage.DbUpdateConcurrencyException, ex);
            }
            catch (DbUpdateException ex)
            {
                // YOU MUST LOG - Habria que hacer un log
                throw new VuelingExceptions(ResourceMessage.DbUpdateException, ex);
            }
            catch (DbEntityValidationException ex)
            {
                // YOU MUST LOG - Habria que hacer un log
                throw new VuelingExceptions(ResourceMessage.DbEntityValidationException, ex);
            }
            catch (NotSupportedException ex)
            {
                // YOU MUST LOG - Habria que hacer un log
                throw new VuelingExceptions(ResourceMessage.NotSupportedException, ex);
            }
            catch (ObjectDisposedException ex)
            {
                // YOU MUST LOG - Habria que hacer un log
                throw new VuelingExceptions(ResourceMessage.ObjectDisposedException, ex);
            }
            catch (InvalidOperationException ex)
            {
                // YOU MUST LOG - Habria que hacer un log
                throw new VuelingExceptions(ResourceMessage.InvalidOperationException, ex);
            }

            return(alumnoEntityAdded);
        }
        public AlumnoEntity Add(AlumnoEntity model)
        {
            Alumno  alumno  = null;
            var     config  = new MapperConfiguration(cfg => cfg.CreateMap <AlumnoEntity, Alumno>());
            IMapper iMapper = config.CreateMapper();

            alumno = iMapper.Map <AlumnoEntity, Alumno>(model);

            try
            {
                db.Alumno.Add(alumno);
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                //You Must LOG
                throw new VuelingException(Resource3.ErAct, ex);
            }
            catch (DbUpdateException ex)
            {
                //You Must LOG
                throw new VuelingException(Resource3.ErAct, ex);
            }
            catch (DbEntityValidationException ex)
            {
                //You Must LOG
                throw new VuelingException(Resource3.ErAct, ex);
            }
            catch (NotSupportedException ex)
            {
                //You Must LOG
                throw new VuelingException(Resource3.ErAct, ex);
            }
            catch (ObjectDisposedException ex)
            {
                //You Must LOG
                throw new VuelingException(Resource3.ErAct, ex);
            }
            catch (InvalidOperationException ex)
            {
                //You Must LOG
                throw new VuelingException(Resource3.ErAct, ex);
            }
            return(model);
        }