@startuml

' client
Actor user
Participant client #Gold

' api
Participant "server" as api #LightSalmon
database "db" as db #LightSalmon

' title
title <font size=30>/v1/rooms</font>

' prerequisites
user -> client  : 로그인
client -> api : jwt
return ok {"Authorization": "56E7E522C3..."}

note right of user
현재 과정 모두 진행된 상태에서 아래 시퀀스들이 진행됩니다.
endnote
|||
== POST /v1/rooms   교환일기방 생성 ==
client -> api++: **POST /v1/rooms**
note right of client #Lavender
{
    "name": 방이름,
    "code": 질문답변,
    "hint": 참여질문,
    "theme": "1"|"2"|"3",
}
endnote

api -> api: json validation
api -> api: orders 필드 추가\
\n{\n\t...\n\t"orders": [현재_로그인_account_id],\n}
api -> db++: insert a room
return room
return **200 Ok**
note right of client #Lavender
{
    "roomId": 1
}
endnote

||20||

== GET /v1/rooms/<:room_id>   교환일기방 상세화면 접근==
' TODO: 서버에서 접근 권한 확인 401(1. 로그인 안된경우 / 2. 멤버가 아닌경우) error일 경우 redirect hint page
' 멤버가 아닌경우, hint 전달

client -> api++: **GET /v1/rooms/<:room_id>?page=<:page>**
note right
?page=는 diary(일기들)의 페이지네이션을 뜻한다.
endnote
api -> db++: Get a Room\
\n**WHERE id = <room_id>**
return room
api -> db++: **교환일기 작성 순서에 따라 계정정보 획득**\
\n(room.orders순서로 account in-query)\
\nSELECT id, profile_url FROM accounts WHERE in <room.orders>
return []member
api -> api: 현재 계정이 멤버인지 확인
alt#Gold #Pink Failure
    api -> client: **401 Unauthorized**
    client -> user: 교환일기방 참여코드 입력 페이지 렌더링
end

api -> db++: Get Diaries(Order by Created_by)\
\nSELECT * FROM diaries WHERE room_id=<room_id> ORDER BY id desc LIMIT 10 OFFSET 10*<page>
note right
page size = 10
endnote
return []diary
return **200 OK**
note right of client #Lavender
{
    "orders": [
        {
            "accountID": "<int>",
            "profileUrl": "<url>" // https://voda-api.com/profiles/leoo.png
        },
        {
            "accountID": "<int>",
            "profileUrl": "<url>"
        }
    ],
    "turnAccountId": "<int>", // 현재 턴 accountID (이걸 기준으로 orders에서 파랑색 동그라미)
    "turnAccountName": "<str>", // 현재 턴 accountName i.g "고영희", ("고영희님이 이야기를 쓰고있어요!"일때 사용)
    "name": "<str>", // 다이어리 이름 i.g. 고영희 미만 다꾸러
    "theme": "<str>", // 다이어리 배경 테마명 i.g. #고영희_자랑하는 일기
    "diaries": {
        "data": [
            {
            "place": "<str>", // 장소 i.g. "방구석에서"
            "accountName": "<str>", // 생성한 accountName
            "profileUrl": "<url>", // 사용자 프로필 이미지 url
            "createdAt": "<datetime>" //생성 일자
            },
            {},
            {}
        ],
        "totalCount": "<int>" // 총 다이어리 숫자 (페이지 요청용)
    }
}
endnote


||20||

== GET /v1/rooms  가입된 다이어리 (Home 화면용)==
client -> api++: **GET /v1/rooms**
api -> db++: 자신이 멤버로 참여하는 다이어리방들 얻기\
\n**SELETE room_id FROM room_members WHERE account_id=<:current_account_id>**
return []room_id
api -> db++: 멤버로 참여하는 다이어리방이거나, 마스터로 참여하는 다이어리방들 Query(order by created_at desc)\
\n given: (room_ids []room_id, account_id: int)\
\nSELECT * FROM diaries WHERE room_id in <room_ids> OR master_id=<account_id>  ORDER BY id desc
return []room
return **200 OK**
note right of client #Lavender
{
    // 최신 생성순으로 정렬되어 return
    "rooms": [
        {
            "id": "<int>", //다이어리룸 id
            "name": "<str>", // 다이어리룸 이름
            "totalMemberCount": <int>, // +2, +4
            "createdAt": <datetime>, // 최근 다이어리 알려주기용
            "members": [
            {
                "accountID": "<int>",
                "profileUrl": "<url>",
            },
            {}
            ]
        },
        {},
        {},
        ...
    ]
}
endnote

||20||

== POST /v1/rooms/<:room_id>/verify  교환일기방 참여코드 체크 ==
user -> client: 다이어리방 참여코드 입력
client -> api++: **POST /v1/rooms/<:room_id>/verify**

note right of client #Lavender
{
    "code": <사용자가 작성한 코드>
}
endnote
api -> api: json validation
api -> db++: Get a room code
return code
api -> api: verify(사용자작성코드, code)
alt#Gold #LightBlue Successful case
    api -> client: **200 OK**
else #Pink Failure
    api -> client: **401 Unauthorized**
end



||20||
== DELETE /v1/rooms/<:room_id> 교환일기방 나가기 ==
' TODO

||20||
@enduml