feat: implement authentication and database modules with relations and group management
Added new authentication strategies (JWT and GitHub OAuth), guards, and controllers. Implemented database module, schema with relations, and group management features, including CRD operations and person-to-group associations. Integrated validation and CORS configuration.
This commit is contained in:
108
backend/src/modules/projects/services/projects.service.ts
Normal file
108
backend/src/modules/projects/services/projects.service.ts
Normal file
@@ -0,0 +1,108 @@
|
||||
import { Injectable, NotFoundException, Inject } from '@nestjs/common';
|
||||
import { eq, and } from 'drizzle-orm';
|
||||
import { DRIZZLE } from '../../../database/database.module';
|
||||
import * as schema from '../../../database/schema';
|
||||
import { CreateProjectDto } from '../dto/create-project.dto';
|
||||
import { UpdateProjectDto } from '../dto/update-project.dto';
|
||||
|
||||
@Injectable()
|
||||
export class ProjectsService {
|
||||
constructor(@Inject(DRIZZLE) private readonly db: any) {}
|
||||
|
||||
/**
|
||||
* Create a new project
|
||||
*/
|
||||
async create(createProjectDto: CreateProjectDto) {
|
||||
const [project] = await this.db
|
||||
.insert(schema.projects)
|
||||
.values(createProjectDto)
|
||||
.returning();
|
||||
return project;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find all projects
|
||||
*/
|
||||
async findAll() {
|
||||
return this.db.select().from(schema.projects);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find projects by owner ID
|
||||
*/
|
||||
async findByOwnerId(ownerId: string) {
|
||||
return this.db
|
||||
.select()
|
||||
.from(schema.projects)
|
||||
.where(eq(schema.projects.ownerId, ownerId));
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a project by ID
|
||||
*/
|
||||
async findById(id: string) {
|
||||
const [project] = await this.db
|
||||
.select()
|
||||
.from(schema.projects)
|
||||
.where(eq(schema.projects.id, id));
|
||||
|
||||
if (!project) {
|
||||
throw new NotFoundException(`Project with ID ${id} not found`);
|
||||
}
|
||||
|
||||
return project;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a project
|
||||
*/
|
||||
async update(id: string, updateProjectDto: UpdateProjectDto) {
|
||||
const [project] = await this.db
|
||||
.update(schema.projects)
|
||||
.set({
|
||||
...updateProjectDto,
|
||||
updatedAt: new Date(),
|
||||
})
|
||||
.where(eq(schema.projects.id, id))
|
||||
.returning();
|
||||
|
||||
if (!project) {
|
||||
throw new NotFoundException(`Project with ID ${id} not found`);
|
||||
}
|
||||
|
||||
return project;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a project
|
||||
*/
|
||||
async remove(id: string) {
|
||||
const [project] = await this.db
|
||||
.delete(schema.projects)
|
||||
.where(eq(schema.projects.id, id))
|
||||
.returning();
|
||||
|
||||
if (!project) {
|
||||
throw new NotFoundException(`Project with ID ${id} not found`);
|
||||
}
|
||||
|
||||
return project;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a user has access to a project
|
||||
*/
|
||||
async checkUserAccess(projectId: string, userId: string) {
|
||||
const [project] = await this.db
|
||||
.select()
|
||||
.from(schema.projects)
|
||||
.where(
|
||||
and(
|
||||
eq(schema.projects.id, projectId),
|
||||
eq(schema.projects.ownerId, userId)
|
||||
)
|
||||
);
|
||||
|
||||
return !!project;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user