boolean-uk / team-dev-server-ex-2410-team3

0 stars 0 forks source link

Backend - Design Cohort - Implement Cohort model in domain and route folders #21

Closed sebgro98 closed 5 hours ago

sebgro98 commented 1 day ago

Related to this issue that address Prisma modelling. To interface the backend database, a model-class of Cohort must be implemented.

It should import dbClient to access the database

import dbClient from '../utils/dbClient.js'

It should be able to return all cohorts

export async function getAllCohorts() {
  try {
    const cohorts = await dbClient.cohort.findMany({
      include: {
        users: { select: { id: true, role: true } }
      }
    })
    return cohorts
  } catch (error) {
    console.error('Error getting cohorts:', error)
    throw error
  }
}

It should be able to create a cohort

export async function createCohort({ name, startDate, endDate }) {
  startDate = new Date(startDate)
  endDate = new Date(endDate)
  try {
    const createdCohort = await dbClient.cohort.create({
      data: {
        name,
        startDate,
        endDate,
        createdAt: new Date(),
        updatedAt: new Date()
      },
      include: {
        users: { select: { id: true, role: true } }
      }
    })
    console.log('Created cohort:', createdCohort)
    return createdCohort
  } catch (error) {
    console.error('Error creating cohort:', error)
    throw error
  }
}
}

The seeding should be addressed also, in prisma/seed.js, by updating the createCohort function:

async function createCohort() {
  const cohort = await prisma.cohort.create({
    data: {
      name: 'Web Development',
      startDate: new Date('2014-06-01'),
      endDate: new Date('2014-12-01'),
      createdAt: new Date(),
      updatedAt: new Date()
    }
  })

  console.info('Cohort created', cohort)

  return cohort
}
sebgro98 commented 1 day ago

Approved

amos1969 commented 1 day ago

Is the Seeding of sample data covered too?

maaxolofsson commented 1 day ago

Is the Seeding of sample data covered too?

Now it is

amos1969 commented 1 day ago

Approved