Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Se agregan peticiones a una base de datos a través del nuevo servicio "CommanderService" #2

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

Drako9159
Copy link

Se habilitó una nueva tabla en la base de datos explorers_api llamada Commander

model Commander {
  id Int @id @default(autoincrement())
  name String @unique
  username String @db.VarChar(255)
  mainStack String @db.VarChar(255)
  currentEnrollment Boolean @default(false)
  hasAzureCertification Boolean @default(false)
}

La tabla recibe las peticiones del cliente con los endpoints

app.get("/commanders", async (req, res) => {
    const commander =  await prisma.commander.findMany({});
    res.json(commander);
});
app.get("/commanders/:id", async (req, res) => {
    const id = req.params.id;
    const commander = await prisma.commander.findUnique({where: {id: parseInt(id)}});
    res.json(commander);
});
app.post("/commanders", async (req, res) => {
    const commander = {
        name: req.body.name,
        username: req.body.username,
        mainStack: req.body.mainStack,
        currentEnrollment: req.body.currentEnrollment,
        hasAzureCertification: req.body.hasAzureCertification
    };
    const message = "missionCommander creado.";
    await prisma.commander.create({data: commander});
    return res.json({message});
});
app.put("/commanders/:id", async (req, res) => {
    const id = parseInt(req.params.id);

    await prisma.commander.update({
        where: {
            id: id
        },
        data: {
            mainStack: req.body.mainStack
        }
    });
    return res.json({message: "Actualizado correctamente"});
});
app.delete("/commanders/:id", async (req, res) => {
    const id = parseInt(req.params.id);
    await prisma.commander.delete({where: {id: id}});
    return res.json({message: "Eliminado correctamente"});
});

En el repo client-launchx se crea un nuevo servicio CommanderService con los métodos para las peticiones

import http from "../http-common";

class CommanderService {
  getAll() {
    return http.get("/commanders");
  }
  get(id) {
    return http.get(`/commanders/${id}`);
  }
  create(data) {
    return http.post("/commanders", data);
  }
  update(id, data) {
    return http.put(`/commanders/${id}`, data);
  }
  delete(id) {
    return http.delete(`/commanders/${id}`);
  }
}

export default new CommanderService();

Se crea un componente para añadir nuevos commanders

CommanderService.create(data)
        .then(response => {
          this.commanders.id = response.data.id;
          this.submitted = true;
        })
        .catch(e => {
          console.log(e);
        });

Se crea un componente para realizar actualizaciones del commander y otro que los elimina

deleteCommander() {
      CommanderService.delete(this.currentCommander.id)
      .then(response => {
        console.log(response.data);
        this.$router.push({ name: "commanders" });
      })
      .catch(e => {
          console.log(e);
      });
    },
    updateCommander() {
      CommanderService.update(this.currentCommander.id, this.currentCommander)
        .then(response => {
          console.log(response.data);
          this.message = 'Se actualizó correctamente';
        })
        .catch(e => {
          console.log(e);
        });
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant