Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…-02-klowhub into backend
  • Loading branch information
sibas1 committed Nov 23, 2024
2 parents 08b2879 + b14cf7e commit 93d5589
Show file tree
Hide file tree
Showing 40 changed files with 853 additions and 340 deletions.
79 changes: 74 additions & 5 deletions server/apps/courses/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions server/apps/courses/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@
"@nestjs/common": "^10.0.0",
"@nestjs/core": "^10.0.0",
"@nestjs/mapped-types": "*",
"@nestjs/microservices": "^10.4.8",
"@nestjs/mongoose": "^10.1.0",
"@nestjs/platform-express": "^10.0.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"cloudinary": "^2.2.0",
"dotenv": "^16.4.5",
"mongoose": "^8.8.1",
"mongoose": "^8.8.2",
"multer": "^1.4.5-lts.1",
"reflect-metadata": "^0.2.0",
"rxjs": "^7.8.1",
"cloudinary": "^2.2.0",
"multer": "^1.4.5-lts.1"
"zod": "^3.23.8"
},
"devDependencies": {
"@nestjs/cli": "^10.0.0",
Expand Down
4 changes: 1 addition & 3 deletions server/apps/courses/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { CoursesModule } from './courses/courses.module'; // Importamos el módulo de cursos
import { LeccionesModule } from './lecciones/lecciones.module';

import * as dotenv from 'dotenv';

Expand All @@ -10,8 +9,7 @@ dotenv.config();
@Module({
imports: [
MongooseModule.forRoot(process.env.MONGO_URI),
CoursesModule,
LeccionesModule
CoursesModule
],
})
export class AppModule {}
20 changes: 0 additions & 20 deletions server/apps/courses/src/clases/dto/clases.dto.ts

This file was deleted.

48 changes: 24 additions & 24 deletions server/apps/courses/src/courses/course.shema.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import { Schema, Document } from 'mongoose';
// import { Schema, Document } from 'mongoose';


export const CourseSchema = new Schema({
name: { type: String, required: true },
description: { type: String, required: true },
createAt:{type: Date, required: true,default:Date.now()},
updateAt:{type: Date, required: true,default:Date.now()},
userId:{type: String, required:true},
price:{type:Number},
plataforma:{type:String},
reputacion:{type:String},
fotoCurso:{type:String},
tags:{type:String}
});
// export const CourseSchema = new Schema({
// name: { type: String, required: true },
// description: { type: String, required: true },
// createAt:{type: Date, required: true,default:Date.now()},
// updateAt:{type: Date, required: true,default:Date.now()},
// userId:{type: String, required:true},
// price:{type:Number},
// plataforma:{type:String},
// reputacion:{type:String},
// fotoCurso:{type:String},
// tags:{type:String}
// });

export interface Course extends Document {
id: string;
name: string;
description: string;
userId:string;
price:number;
plataforma:string;
reputacion:string;
fotoCurso:string;
tags:string;
}
// export interface Course extends Document {
// id: string;
// name: string;
// description: string;
// userId:string;
// price:number;
// plataforma:string;
// reputacion:string;
// fotoCurso:string;
// tags:string;
// }
43 changes: 30 additions & 13 deletions server/apps/courses/src/courses/courses.controller.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,42 @@
import { Controller, Post, Body,Get, Param } from '@nestjs/common';
import { Controller } from '@nestjs/common';
import { CoursesService } from './courses.service';
import { CreateCourseDto } from './dto/create-course.dto'; // Importa el DTO
import { MessagePattern, RpcException } from '@nestjs/microservices';
import { CreateCourseSchema } from './dto/create-course.dto';

@Controller('courses')
export class CoursesController {
constructor(private readonly coursesService: CoursesService) {}

@MessagePattern({ cmd: 'createCourse' })
async createCourse(courseData: any) {
// Validar los datos con Zod
const validationResult = CreateCourseSchema.safeParse(courseData);
if (!validationResult.success) {
throw new RpcException({
statusCode: 400,
message: 'Validation error',
errors: validationResult.error.errors,
});
}

@Get('/')
async getCourse(){
return await this.coursesService.getCourse();
// Llamar al servicio para guardar el curso
return this.coursesService.createCourse(validationResult.data);
}

@Get('/:id')
async getCourseId(@Param('id') id:string) {
return await this.coursesService.getCourseId(id);
}
// @Get('/')
// async getCourse(){
// return await this.coursesService.getCourse();
// }

@Post()
async create(@Body() createCourseDto: CreateCourseDto){
return await this.coursesService.create(createCourseDto);
}
// @Get('/:id')
// async getCourseId(@Param('id') id:string) {
// return await this.coursesService.getCourseId(id);
// }

// @Post()
// async create(@Body() createCourseDto: CreateCourseDto){
// return await this.coursesService.create(createCourseDto);
// }

// Otros métodos del controlador (por ejemplo, para obtener cursos, eliminar, etc.)
}
5 changes: 3 additions & 2 deletions server/apps/courses/src/courses/courses.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { Course, CourseSchema } from './schemas/course.schema';
import { CoursesService } from './courses.service';
import { CoursesController } from './courses.controller';
import { CourseSchema } from './course.shema'; // Importa el esquema


@Module({
imports: [
MongooseModule.forFeature([{ name: 'Course', schema: CourseSchema }]), // Registra el modelo en el módulo
MongooseModule.forFeature([{ name: Course.name, schema: CourseSchema }]), // Registra el modelo en el módulo
],
providers: [CoursesService],
controllers: [CoursesController],
Expand Down
44 changes: 27 additions & 17 deletions server/apps/courses/src/courses/courses.service.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,37 @@
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { Course } from './course.shema';
import { CreateCourseDto } from './dto/create-course.dto';
import { Course } from './schemas/course.schema';

@Injectable()
export class CoursesService {
constructor(
@InjectModel('Course') private readonly courseModel: Model<Course>, // Inyecta el modelo Course
) {}
constructor(@InjectModel(Course.name) private courseModel: Model<Course>) {}

async getCourse(){
return this.courseModel.find();
}
async getCourseId(id){
return await this.courseModel.findById(id);
async createCourse(data: any) {
const newCourse = new this.courseModel(data);
return newCourse.save();
}
}
//constructor(@InjectModel(Course.name) private courseModel: Model<Course>) { }

// Método para crear un curso
async create(createCourseDto: CreateCourseDto): Promise<Course> {
const course = new this.courseModel(createCourseDto); // Usa el DTO para crear el curso
return course.save(); // Guarda el curso en la base de datos
}
// async createCourse(data: { title: string; description?: string }) {
// const newCourse = new this.courseModel(data);
// return newCourse.save();
// }

// async getAllCourses() {
// return this.courseModel.find().exec();
// }

// async getCourse(){
// return this.courseModel.find();
// }
// async getCourseId(id){
// return await this.courseModel.findById(id);
// }

// Otros métodos de servicio (por ejemplo, para listar cursos, eliminar, etc.)
}
// // Método para crear un curso
// async create(createCourseDto: CreateCourseDto): Promise<Course> {
// const course = new this.courseModel(createCourseDto); // Usa el DTO para crear el curso
// return course.save(); // Guarda el curso en la base de datos
// }
Loading

0 comments on commit 93d5589

Please sign in to comment.