@startuml Manage CoworkingSpace (PUT)

header Coworking Space Reservation System Sequence Diagram
footer Group Yossawat & Supanut
title "Manage CoworkingSpace (PUT)"

participant "Client" as client
participant "<<javaScript>>\n:server" as server
participant "<<router>>\n:coworkingspaces" as routerCoworkingSpaces
participant "<<middleware>>\n:auth" as middleware
participant "<<controllers>>\n:coworkingspaces" as controllersCoworkingSpaces
participant "<<model>>\n:CoworkingSpace" as modelCoworkingSpace
database "<<MySQL>>\n:coworkingspaces" as CoworkingSpacesDatabase

client->server ++:req.put('/coworkingspaces/:id')
server->routerCoworkingSpaces ++:app.use('/coworkingspaces',coworkingspaces)
routerCoworkingSpaces -> middleware ++:protect()\nauthorize('admin')
alt Already logged in and correct role
    middleware -> controllersCoworkingSpaces ++: updateCoworkingSpace()
else Not logged in yet
    middleware --> client : res.status(401).json({ sucess: false, message: 'Not authorize to access this route' })
else Wrong role
    middleware --> client --: res.status(403).json({ sucess: false, message: `User role ${req.user.role} is not authorized to access this route` })
end
controllersCoworkingSpaces-> modelCoworkingSpace ++:update(req.params.id, req.body)
modelCoworkingSpace -> CoworkingSpacesDatabase ++: CoworkingSpaceSchema
CoworkingSpacesDatabase --> modelCoworkingSpace --: modifiedCoworkingSpace
controllersCoworkingSpaces <-- modelCoworkingSpace --: modifiedCoworkingSpace
controllersCoworkingSpaces-->client --: res.status(200).json({ success: true, data: modifiedCoworkingSpace.dataValues })

@enduml
