Samagra-Development / Doc-Generator

Create PDFs from a variety of formats.
17 stars 46 forks source link

Add batch creation API #152

Open AnshulMalik opened 1 year ago

AnshulMalik commented 1 year ago

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

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

Swatishree-Mahapatra commented 1 year ago

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

Swatishree-Mahapatra commented 1 year ago

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 commented 1 year ago

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 :)