Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion devU-api/src/database.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ConnectionOptions } from 'typeorm'
import { ConnectionOptions, Repository } from 'typeorm'

import environment from './environment'

Expand All @@ -23,3 +23,31 @@ const typeORMConfiguration: ConnectionOptions = {
}

export default typeORMConfiguration


/*
This function is used to group the data by the specified column
@param connection: the specific connection to the database
@param columnList: the list of columns to be used to group the data
@param query: the query object
@param filter: the filter object
@returns the grouped data
*/
export async function groupBy<T>( connection: Repository<T>, columnList:string[], query: any, filter: { index: string, value: number }){
let orders = query
// The filteredOrders currently only filters the orders by the columnList, any other orders are removed
// and only set to 'ASC' since no input is provided for the order
const filteredOrders = Object.entries(orders)
.filter(([key]) => columnList.includes(orders[key]))
.reduce((acc, [key]) => ({ ...acc, [orders[key]]: 'ASC' }), {});

orders = Object.keys(filteredOrders).length === 0 ? { id: 'ASC' } : filteredOrders;

return await connection.find({
where: {
[filter.index]: filter.value
},
order: orders,
withDeleted: false
})
}
6 changes: 6 additions & 0 deletions devU-api/src/entities/submission/submission.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import { serialize } from './submission.serializer'

export async function get(req: Request, res: Response, next: NextFunction) {
try {
const userId = req.currentUser?.userId
const query = req.query

if (!userId) return res.status(400).json(new GenericResponse('Request requires auth'))

const submissions = await SubmissionService.list(query, userId)
const assignmentId = req.query.assignment as number | undefined
const userId = req.query.user as number | undefined

Expand Down
16 changes: 7 additions & 9 deletions devU-api/src/entities/submission/submission.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import CourseModel from '../course/course.model'

import { Submission, FileUpload } from 'devu-shared-modules'
import { uploadFile } from '../../fileStorage'
import { groupBy } from '../../database'

const submissionConn = () => getRepository(SubmissionModel)
const fileConn = () => getRepository(FileModel)
Expand Down Expand Up @@ -43,15 +44,12 @@ export async function retrieve(id: number) {
return await submissionConn().findOne({ id, deletedAt: IsNull() })
}

export async function list(assignmentId?: number, userId?: number) {
const options: FindManyOptions = {
where: {
deletedAt: IsNull(),
...(assignmentId !== undefined && {assignmentId}),
...(userId !== undefined && {userId})
}
}
return await submissionConn().find(options)


export async function list(query: any, id: number) {
const OrderByMappings = ['id', 'createdAt', 'updatedAt', 'courseId', 'assignmentId', 'submittedBy']

return await groupBy<SubmissionModel>(submissionConn(), OrderByMappings, query, { index: 'submittedBy', value: id })
}

export default {
Expand Down
2 changes: 1 addition & 1 deletion devU-api/src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const environment = {

// MinIO setting
minioHost: isDocker ? load('minio.host') : 'localhost' as string,
minioPort: (load('minio.port') || 9002) as number,
minioPort: isDocker ? load('minio.port') : 9002 as number,
minioUsername: (load('minio.username') || 'typescript_user') as string,
minioPassword: (load('minio.password') || 'changeMe') as string,

Expand Down