Feature/day 7 todos notes backend junaid#16
Conversation
…ty (order by creation, update date & title)
|
|
||
|
|
||
| async update({ params, request, response }: HttpContext) { | ||
| const todo = await Todo.findOrFail(params.id) |
|
|
||
|
|
||
| async store({ request, response }: HttpContext) { | ||
| const data = request.only(['title', 'content', 'labels', 'imageUrl']) |
There was a problem hiding this comment.
if you add validation here, then you dont have to add validation in the parse function, You are using Typescript, instead of adding type any, you can define multiple types like
labels: string | undefined | ...
| private parseLabels(labels: any): string[] | null { | ||
| if (!labels) return null | ||
|
|
||
| if (Array.isArray(labels)) { |
There was a problem hiding this comment.
why do you need multiple types?
| return response.badRequest({ error: 'No image file provided' }) | ||
| } | ||
|
|
||
| const result = await cloudinary.uploader.upload(payload.image.tmpPath!, { |
There was a problem hiding this comment.
best approach is to create CloudnaryService class.
pass image path and folder name then call the upload function.
your code should look like.
await new CloudnaryService(imgPath, folder_name).upload();
| folder: 'adonis_uploads', | ||
| }) | ||
|
|
||
| return response.ok({ message: 'Image uploaded successfully', url: result.secure_url }) |
There was a problem hiding this comment.
What if Cloudinary throw an exception?
how you will tackle this?
| return JSON.stringify(value) | ||
| } | ||
| }) | ||
| declare labels: string[] | null |
There was a problem hiding this comment.
The label is a string[] or null type in the model, pass the type instead of any, and apply validation in your controller.
|
|
||
| if (editingTodo) { | ||
| // Update existing todo | ||
| const updatedTodo: Todo = { |
There was a problem hiding this comment.
what about frontend validation?
| export default function Show({ todo }: { todo: Todo }) { | ||
| const [isDeleting, setIsDeleting] = useState(false) | ||
|
|
||
| const formatDate = (dateString: string) => { |
There was a problem hiding this comment.
move this function in the utils folder, and import wherever needed
| } | ||
|
|
||
| export default function TodoCard({ todo, viewType, onEdit, onDelete }: TodoCardProps) { | ||
| const formatDate = (dateString: string) => { |
There was a problem hiding this comment.
code duplication, move in the utilis
| router.get('/todos', ({ inertia }) => inertia.render('todos/empty')) | ||
| // router.get('/todos', ({ inertia }) => inertia.render('todos/empty')) | ||
|
|
||
| router.get('/todos', [TodosController, 'index']) // Fetch all todos |
There was a problem hiding this comment.
use route groups
https://docs.adonisjs.com/guides/basics/routing#grouping-routes
| } | ||
| } catch (error) { | ||
| console.error('Cloudinary Delete Error:', error) | ||
| throw new Error('Failed to delete image from Cloudinary') |
There was a problem hiding this comment.
use error.message or custom message like
error?.message ?? "Failed to delete image from Cloudinary"
it means, if error.message is undefined then return the fallback message.
| }); | ||
|
|
||
|
|
||
| const TodoSchema = z.object({ |
…frontend-junaid Feature/day 6 project notes frontend junaid
No description provided.