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

Add batch creation API #152

Open
AnshulMalik opened this issue Jun 4, 2023 · 3 comments
Open

Add batch creation API #152

AnshulMalik opened this issue Jun 4, 2023 · 3 comments
Assignees
Labels

Comments

@AnshulMalik
Copy link
Collaborator

We need a POST /batches API for creating batch jobs.

Here is the spec for it: https://gist.github.com/AnshulMalik/513723d253ea5ef4ff37795da1dde4f1

@AnshulMalik AnshulMalik added the good first issue Good for newcomers label Jun 4, 2023
@Swatishree-Mahapatra
Copy link
Contributor

Swatishree-Mahapatra commented Jun 6, 2023

I would like to work on it. Could you please assign it?

@Swatishree-Mahapatra
Copy link
Contributor

Hello @AnshulMalik! I have worked on an API that uses express.js and connects to a MongoDB database (that can be changed whenever required according to the database of Doc-Generator). For a demo, all the parts are in a single file. I will divide it into Model, Controller, and Router structures, once verified. Could you please review if the following is the required deliverable for this issue or if I have understood it wrong?

const express = require('express');
const router = express.Router();
const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/BatchDB', {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});

const BatchSchema = new mongoose.Schema({
  id: { type: Number, required: true },
  name: { type: String, required: true },
});

const Batch = mongoose.model('Batch', BatchSchema);

router.post('/post/batches', async (req, res) => {
  try {
    const batchData = req.body;
    const { error } = validateBatchRequest(batchData);
    if (error) {
      res.status(400).json({ error: error.details[0].message });
      return;
    }

    const newBatch = new Batch(batchData);
    await newBatch.save();
    const response = {
      message: 'Batch submitted successfully',
    };
    res.status(200).json(response);
  } 
  catch (error) {
    console.error('Error submitting batch:', error);
    res.status(500).json({ error: 'Internal Server Error' });
  }
});

const validateBatchRequest = (batchData) => {
  const schema = Joi.object({
    id: Joi.number().integer().positive().required(),
    name: Joi.string().required(),
  });

  return schema.validate(batchData);
};

module.exports = router;

@AnshulMalik
Copy link
Collaborator Author

Hey @Swatishree-Mahapatra you're on right track , we need to do something similar. Some points:

  1. We are using prisma, so monogdb won't be needed.
  2. All the database related things will go through prisma service. There will be a database representation of a Batch inside of prisma service.
  3. Then there will be APIs on top of that, these will not return the prisma service db representation object, but will have their own DTO (google this).
  4. No need of adding post in url, just /batches will work.

Also, it's better if we have a PR, we can discuss there, as it's more suitable place for these discussions :)

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

No branches or pull requests

2 participants