Exemplo n.º 1
0
        public async Task <IHttpActionResult> PostCliente(DTO_Cliente cliente)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (db.Clientes.Any(x => x.Nombre == cliente.Nombre))
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "El cliente ya está registrado"));
            }

            Cliente nuevoCliente = new Cliente()
            {
                Nombre        = cliente.Nombre,
                FechaCreacion = DateTime.Now
            };

            db.Clientes.Add(nuevoCliente);
            await db.SaveChangesAsync();

            cliente.Id = nuevoCliente.Id;

            return(CreatedAtRoute("DefaultApi", new { id = cliente.Id }, cliente));
        }
Exemplo n.º 2
0
        public async Task <IHttpActionResult> GetCliente(int id)
        {
            Cliente c = await db.Clientes.FindAsync(id);

            if (c == null)
            {
                return(NotFound());
            }

            DTO_Cliente cliente = new DTO_Cliente()
            {
                Id            = c.Id,
                Nombre        = c.Nombre,
                FechaCreacion = c.FechaCreacion
            };

            return(Ok(cliente));
        }
Exemplo n.º 3
0
        public async Task <IHttpActionResult> PutCliente(int id, DTO_Cliente cliente)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

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

            if (db.Clientes.Any(x => x.Nombre == cliente.Nombre))
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "El cliente ya está registrado"));
            }

            Cliente actualizaCliente = await db.Clientes.FindAsync(id);

            actualizaCliente.Nombre = cliente.Nombre;

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

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ClienteExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Exemplo n.º 4
0
        public async Task <IHttpActionResult> DeleteCliente(int id)
        {
            Cliente cliente = await db.Clientes.FindAsync(id);

            if (cliente == null)
            {
                return(NotFound());
            }

            DTO_Cliente eliminaCliente = new DTO_Cliente()
            {
                Id            = cliente.Id,
                Nombre        = cliente.Nombre,
                FechaCreacion = cliente.FechaCreacion
            };

            db.Clientes.Remove(cliente);
            await db.SaveChangesAsync();

            return(Ok(eliminaCliente));
        }