Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add course fixed , need to change logic with sqs #77

Merged
merged 1 commit into from
Sep 9, 2023
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
68 changes: 52 additions & 16 deletions client/src/components/pages/add-course/add-course-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,15 @@ interface CourseFormValues {
[key: string]: string;
}


const initialValues = {
title: "",
duration: "",
category: "",
level:"",
tags: "",
about:"",
level: "",
tags: "",
about: "",
description: "",
syllabus:"",
syllabus: "",
requirements: "",
price: "",
};
Expand All @@ -38,21 +37,29 @@ const CombinedForm: React.FC = () => {
const [paid, setPaid] = useState(false);
const [thumbnail, setThumbnail] = useState<File | null>(null);
const [guidelines, setGuidelines] = useState<File | null>(null);
const [introduction,setIntroduction] = useState<File | null>(null)
const [categories, setCategories] = useState<ApiResponseCategory[] | null>(
null
);

const handleFormSubmit = async (values: CourseFormValues, { resetForm }: FormikHelpers<CourseFormValues>) => {
const handleFormSubmit = async (
values: CourseFormValues,
{ resetForm }: FormikHelpers<CourseFormValues>
) => {
try {
const formData = new FormData();
guidelines && formData.append("files", guidelines);
thumbnail && formData.append("files", thumbnail);
introduction && formData.append("files",introduction)
Object.keys(values).forEach((key) => formData.append(key, values[key]));
const response = await addCourse(formData);
toast.success(response.data.message, {
toast.success(response.data.message, {
position: toast.POSITION.BOTTOM_RIGHT,
});
resetForm()
resetForm();
setGuidelines(null)
setThumbnail(null)
setIntroduction(null)
} catch (error: any) {
toast.error(error.data.message, {
position: toast.POSITION.BOTTOM_RIGHT,
Expand All @@ -65,7 +72,7 @@ const CombinedForm: React.FC = () => {
const response = await getAllCategories();
setCategories(response.data);
} catch (error) {
toast.error("something went wrong")
toast.error("something went wrong");
}
};

Expand Down Expand Up @@ -144,9 +151,11 @@ const CombinedForm: React.FC = () => {
name='category'
className='pl-2 block w-80 rounded-md border-0 py-2.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 focus:ring-2 focus:ring-inset focus:ring-indigo-700 focus-visible:outline-none focus-visible:ring-blue-600 sm:text-sm sm:leading-6'
>
{categories?.map(({ _id, name },index) => (
<option selected={index===0} key={_id}>{name}</option>
))}
{categories?.map(({ _id, name }, index) => (
<option selected={index === 0} key={_id}>
{name}
</option>
))}
</Field>
<ErrorMessage
name='category'
Expand All @@ -168,7 +177,9 @@ const CombinedForm: React.FC = () => {
name='level'
className='pl-2 block w-80 rounded-md border-0 py-2.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 focus:ring-2 focus:ring-inset focus:ring-indigo-700 focus-visible:outline-none focus-visible:ring-blue-600 sm:text-sm sm:leading-6'
>
<option value='easy' selected>Easy</option>
<option value='easy' selected>
Easy
</option>
<option value='medium'>Medium</option>
<option value='hard'>Hard</option>
</Field>
Expand Down Expand Up @@ -208,7 +219,7 @@ const CombinedForm: React.FC = () => {
</div>

{paid && (
<div className=''>
<div className='mb-2'>
<label
htmlFor='price'
className='block text-sm font-medium leading-6 text-gray-900'
Expand All @@ -228,6 +239,32 @@ const CombinedForm: React.FC = () => {
/>
</div>
)}

<div className='mb-2'>
<label
htmlFor='introduction-video'
className='block text-sm font-medium leading-6 text-gray-900'
>
Introduction video
</label>
<input
type='file'
id='introduction-video'
name='introduction-video'
accept='video/*'
onChange={(event) => {
const file = event.target.files?.[0] || null;
setIntroduction(file);
}}
required
className='pl-2 block w-80 rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 focus:ring-2 focus:ring-inset focus:ring-indigo-700 focus-visible:outline-none focus-visible:ring-blue-600 sm:text-sm sm:leading-6'
/>
<ErrorMessage
name='introduction-video'
component='div'
className='text-red-500 text-sm'
/>
</div>
</div>
</div>
<div>
Expand Down Expand Up @@ -341,7 +378,6 @@ const CombinedForm: React.FC = () => {
/>
</div>
</div>

<div>
<div className='mb-2'>
<label
Expand All @@ -353,7 +389,7 @@ const CombinedForm: React.FC = () => {
<input
type='file'
id='thumbnail'
name='thumbnail'
name='thumbnail'
accept='image/*'
required
onChange={(event) => {
Expand Down
3 changes: 2 additions & 1 deletion server/src/adapters/controllers/courseController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ const courseController = (
cloudService,
dbRepositoryCourse
);
res.status(200).json({
console.log(response)
res.status(201).json({
status: 'success',
message:
'Successfully added new course, course will be published after verification',
Expand Down
56 changes: 30 additions & 26 deletions server/src/app/usecases/course/addCourse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,51 @@ export const addCourses = async (
cloudService: ReturnType<CloudServiceInterface>,
courseDbRepository: ReturnType<CourseDbRepositoryInterface>
) => {
if (!instructorId) {
throw new AppError('Please provide instructor id', HttpStatusCodes.BAD_REQUEST);
if (!instructorId || !courseInfo || !files || files.length === 0) {
throw new AppError('Invalid input data', HttpStatusCodes.BAD_REQUEST);
}
console.log(files);

if (!courseInfo) {
throw new AppError('Please provide course details', HttpStatusCodes.BAD_REQUEST);
}
const uploadPromises = files.map(async (file) => {
let uploadedFile;

if (files && files.length > 0) {
const uploadPromises = files.map(async (file) => {
if (file.mimetype === 'application/pdf') {
const guidelines = await cloudService.upload(file);
courseInfo.guidelines = guidelines;
} else {
const thumbnail = await cloudService.upload(file);
courseInfo.thumbnail = thumbnail;
}
});

await Promise.all(uploadPromises);
}
if (file.mimetype === 'application/pdf') {
uploadedFile = await cloudService.upload(file);
courseInfo.guidelines = uploadedFile;
}

if (file.mimetype === 'video/mp4') {
uploadedFile = await cloudService.upload(file);
courseInfo.introduction = uploadedFile;
}

if (file.mimetype.includes('image')) {
uploadedFile = await cloudService.upload(file);
courseInfo.thumbnail = uploadedFile;
}
});

await Promise.all(uploadPromises);

courseInfo.instructorId = instructorId;

if (typeof courseInfo.tags==='string') {
if (typeof courseInfo.tags === 'string') {
courseInfo.tags = courseInfo.tags.split(',');
}

if (typeof courseInfo.syllabus==='string') {
if (typeof courseInfo.syllabus === 'string') {
courseInfo.syllabus = courseInfo.syllabus.split(',');
}

if (typeof courseInfo.requirements==='string') {
if (typeof courseInfo.requirements === 'string') {
courseInfo.requirements = courseInfo.requirements.split(',');
}


console.log(courseInfo)
const courseId = await courseDbRepository.addCourse(courseInfo);

if (!courseId) {
throw new AppError('Unable to add course, please try again', HttpStatusCodes.INTERNAL_SERVER_ERROR);
throw new AppError(
'Unable to add course',
HttpStatusCodes.INTERNAL_SERVER_ERROR
);
}

return courseId;
Expand Down
4 changes: 4 additions & 0 deletions server/src/frameworks/database/mongodb/models/course.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ const courseSchema = new mongoose.Schema({
type: String,
default: ''
},
introduction: {
type: FileSchema,
required: true
},
coursesEnrolled: [
{
type: mongoose.Schema.Types.ObjectId,
Expand Down
1 change: 1 addition & 0 deletions server/src/types/courseInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface AddCourseInfoInterface {
syllabus:string[]|string;
requirements:string[]|string;
thumbnail: FileSchema;
introduction:FileSchema;
guidelines:FileSchema;
instructorId: string;
rating: number;
Expand Down
Loading