Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions pages/api/sum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { sum } from "@/pages/utils/sum";
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use alias for sum sum: calculateSum


export default function handler(req: NextApiRequest, res: NextApiResponse) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function should have return type e.g

export default function handler(req: NextApiRequest, res: NextApiResponse): void {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate the return type but I think this is not necessary.

const { num1, num2 } = req.query;
const result = sum(num1, num2);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const sum = calculateSum(num1, num2)
res.status(200).json({ sum })

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you provide more context to this? or what do you really mean to do with this?

Copy link
Copy Markdown

@arielpasilang arielpasilang Feb 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add try catch handler and handle statusCode.

  try {
const { num1, num2 } = req.query;
  const result = sum(num1, num2);
res.status(200).json({ statusCode: 200, message: "Success", total: result });
  } catch (err) {
    res.status(400).json({ statusCode:400, message: "Error when summing up", total: null });
  }


res.status(200).json({ sum: result });
}
8 changes: 8 additions & 0 deletions pages/utils/sum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function sum(a: number, b: number, format: "html" | "text" = "html") {
Copy link
Copy Markdown

@thisisdajaaa thisisdajaaa Feb 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kindly create a typing that contains the format value as shown below

type Format = "html" | "text"

And use the format type in the format parameter as a type

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Will do.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's best to add a comment for this function e.g jsdocs to have descriptions for the functionality of this function

Copy link
Copy Markdown
Member Author

@dorelljames dorelljames Feb 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. lets do that. Can you give me reference to JSDocs and how we can do that? Maybe you can also provide an example or reference link for me to check?

Copy link
Copy Markdown

@thisisdajaaa thisisdajaaa Feb 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's best to rename the function to "getSum" or "calculateSum" in order to know that this function calculates the sum

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I think this aligns with our coding standards / style guide.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function should have return type e.g

export function sum(a: number, b: number, format: "html" | "text" = "html"):  string | number {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we only output string here so this makes the type incorrect. Don't you think?

Copy link
Copy Markdown

@thisisdajaaa thisisdajaaa Feb 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's better to rename parameter to "num1" or "num2" to make it more readable compared to a and b. What do you think?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I agree. Lets do that.

Comment thread
dorelljames marked this conversation as resolved.
const result = a + b;
if (format === "html") {
return `<span>${result}</span>`;
} else {
return result;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be much more readable to implement this code block as shown below?

if(format === "text") return result:

return <span>${result}</span>

Copy link
Copy Markdown
Member Author

@dorelljames dorelljames Feb 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we remove the else statement in this case? I think the code you provided behaves the same way with my implementation but I agree that it makes it cleaner if we remove the else at this point.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to expand @DAJAKMPM no need for else statement

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

}
}