Skip to content

Commit

Permalink
Added Penalty Functionality As Instructed
Browse files Browse the repository at this point in the history
  • Loading branch information
mdrehan369 committed May 13, 2024
1 parent 3257209 commit 700219d
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions New_APIs/LibraryApi/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,22 @@ let borrowersData = [
id: 1,
name: 'Md Rehan',
book: 2,
date: new Date()
date: 20,
validity: 5
},
{
id: 2,
name: 'Khushee',
book: 4,
date: new Date()
date: new Date().getDate(),
validity: 5
},
{
id: 3,
name: 'Om Kumar',
book: 3,
date: new Date()
date: new Date().getDate(),
validity: 5
},
]

Expand Down Expand Up @@ -93,8 +96,8 @@ app.post('/api/v1/books', (req, res) => {
app.post('/api/v1/borrowers', (req, res) => {
try {

const { name, book } = req.body;
if( !name || !book ) throw new Error("No Name Or Book Given")
const { name, book, validity } = req.body;
if( !name || !book || !validity ) throw new Error("No Name Or Book Or Expiry Given")

let isAvailable = false
data.map((bk, index) => {
Expand All @@ -105,7 +108,7 @@ app.post('/api/v1/borrowers', (req, res) => {
})

if(isAvailable) {
borrowersData.push({ id: borrowersData.length + 1, name, book, date: new Date() })
borrowersData.push({ id: borrowersData.length + 1, name, book, date: new Date().getDate(), validity });
return res.status(200).json({ success: true, message: 'Borrower Added Successfully' })
} else {
throw new Error("No Book Found")
Expand All @@ -129,10 +132,21 @@ app.get('/api/v1/return/:id', (req, res) => {
if(bdata.id == id) isThere = true, book = bdata.book
})

let penalty = false

if(isThere) {
borrowersData = borrowersData.filter((bdata) => bdata.id != id)
borrowersData = borrowersData.filter((bdata) => {
if(bdata.id == id) {
const currDate = new Date().getDate()
if(( bdata.date < currDate && currDate - bdata.date > bdata.validity ) || ( bdata.date > currDate && 30 - bdata.date + currDate > bdata.validity )) {
penalty = true
}
}
return bdata.id != id
})
data[book - 1].availableCount++;
res.status(200).json({ success: true, message: "Record Deleted Successfully" })
if(penalty) return res.status(200).json({ success: true, message: "Record Deleted Successfully", penalty })
res.status(200).json({ success: true, message: "Record Deleted Successfully", penalty })
} else {
throw new Error("No Record Found")
}
Expand Down

0 comments on commit 700219d

Please sign in to comment.