From 858096d0b1b9fe4e623f7549b9a5c103c5556730 Mon Sep 17 00:00:00 2001 From: wenja0618 Date: Sun, 22 Nov 2026 22:14:16 -0500 Subject: [PATCH] Add Note Board API and repository Split out of #6 (retrofit networking layer). Co-Authored-By: Claude Opus 5.5 --- .../com/example/roomie/data/NoteRepository.kt | 68 ++++++++++++++ .../com/example/roomie/data/api/ApiModels.kt | 18 ++++ .../example/roomie/data/api/NoteApiService.kt | 92 +++++++++++++++++++ .../roomie/data/api/RetrofitInstance.kt | 1 + 4 files changed, 179 insertions(+) create mode 100644 app/src/main/java/com/example/roomie/data/NoteRepository.kt create mode 100644 app/src/main/java/com/example/roomie/data/api/NoteApiService.kt diff --git a/app/src/main/java/com/example/roomie/data/NoteRepository.kt b/app/src/main/java/com/example/roomie/data/NoteRepository.kt new file mode 100644 index 0000000..36cb57e --- /dev/null +++ b/app/src/main/java/com/example/roomie/data/NoteRepository.kt @@ -0,0 +1,68 @@ +package com.example.roomie.data + +import com.example.roomie.data.api.CreateNoteBody +import com.example.roomie.data.api.NoteApiService +import com.example.roomie.data.api.NoteBoardResponse +import com.example.roomie.data.api.NoteColorDto +import com.example.roomie.data.api.NoteDto +import com.example.roomie.data.api.PinNoteBody +import com.example.roomie.data.api.ReportNoteBody +import com.example.roomie.data.api.RetrofitInstance +import com.example.roomie.data.api.RoomieApiResponse +import com.example.roomie.data.api.UpdateNoteBody +import com.example.roomie.data.api.apiCall +import com.example.roomie.data.api.ResponseFlow +import com.example.roomie.data.api.repositoryScope +import kotlinx.coroutines.flow.StateFlow +import java.time.Instant +import javax.inject.Inject +import javax.inject.Singleton + +/** The backend doesn't serve the Note Board yet; see [NoteApiService]. */ +@Singleton +class NoteRepository @Inject constructor(private val retrofitInstance: RetrofitInstance) { + + private val scope = repositoryScope() + private val api get() = retrofitInstance.noteApi + + private val _noteBoard = ResponseFlow(scope) + + /** The first page of the board. Refreshed after every change below succeeds. */ + val noteBoard: StateFlow> = _noteBoard.state + + /** Pipelines the first page into [noteBoard]. */ + fun fetchNoteBoard(houseId: Int, refresh: Boolean = false) { + _noteBoard.fetch(refresh) { api.getNotes(houseId, before = null) } + } + + /** Older notes. Pass the previous page's `nextCursor` as [before]. Doesn't touch [noteBoard]. */ + suspend fun getOlderNotes(houseId: Int, before: Int): RoomieApiResponse = + apiCall { api.getNotes(houseId, before) } + + suspend fun createNote( + houseId: Int, + body: String, + color: NoteColorDto? = null, + pinnedUntil: Instant? = null, + ): RoomieApiResponse = + changing(houseId) { api.createNote(houseId, CreateNoteBody(body.trim(), color, pinnedUntil)).note } + + suspend fun updateNote(houseId: Int, noteId: Int, body: String? = null, color: NoteColorDto? = null): RoomieApiResponse = + changing(houseId) { api.updateNote(houseId, noteId, UpdateNoteBody(body?.trim(), color)).note } + + suspend fun deleteNote(houseId: Int, noteId: Int): RoomieApiResponse = + changing(houseId) { api.deleteNote(houseId, noteId) } + + /** Leave [until] null to pin until someone unpins it. */ + suspend fun pinNote(houseId: Int, noteId: Int, until: Instant? = null): RoomieApiResponse = + changing(houseId) { api.pinNote(houseId, noteId, PinNoteBody(until)).note } + + suspend fun unpinNote(houseId: Int, noteId: Int): RoomieApiResponse = + changing(houseId) { api.unpinNote(houseId, noteId).note } + + suspend fun reportNote(houseId: Int, noteId: Int, reason: String? = null): RoomieApiResponse = + apiCall { api.reportNote(houseId, noteId, ReportNoteBody(reason)) } + + private suspend fun changing(houseId: Int, call: suspend () -> T): RoomieApiResponse = + apiCall(call).also { it.ifSuccess { fetchNoteBoard(houseId, refresh = true) } } +} diff --git a/app/src/main/java/com/example/roomie/data/api/ApiModels.kt b/app/src/main/java/com/example/roomie/data/api/ApiModels.kt index d0fc1c5..fceba6b 100644 --- a/app/src/main/java/com/example/roomie/data/api/ApiModels.kt +++ b/app/src/main/java/com/example/roomie/data/api/ApiModels.kt @@ -21,6 +21,9 @@ enum class ShoppingCategory { GROCERIES, HOUSEHOLD, OTHER } enum class ShoppingStatus { NEEDED, BOUGHT } +/** Proposed; the Note Board isn't built on the backend yet. */ +enum class NoteColorDto { YELLOW, PINK, BLUE, GREEN, PURPLE } + /** The public view of a person: members, rotation, assignee, claimedBy, … */ data class UserDto( val id: Int, @@ -131,3 +134,18 @@ data class ShoppingItemDto( val createdAt: Instant, val updatedAt: Instant, ) + +data class NoteDto( + val id: Int, + /** 1 to 500 characters. */ + val body: String, + val color: NoteColorDto, + /** Compare `author.id` with your own ID to show Edit and Delete. */ + val author: UserDto, + val pinned: Boolean, + /** When the pin ends. Null on a pinned note means until someone unpins it. */ + val pinnedUntil: Instant?, + val createdAt: Instant, + /** Later than [createdAt] means it was edited. */ + val updatedAt: Instant, +) diff --git a/app/src/main/java/com/example/roomie/data/api/NoteApiService.kt b/app/src/main/java/com/example/roomie/data/api/NoteApiService.kt new file mode 100644 index 0000000..988fb2b --- /dev/null +++ b/app/src/main/java/com/example/roomie/data/api/NoteApiService.kt @@ -0,0 +1,92 @@ +package com.example.roomie.data.api + +import retrofit2.http.Body +import retrofit2.http.DELETE +import retrofit2.http.GET +import retrofit2.http.PATCH +import retrofit2.http.POST +import retrofit2.http.Path +import retrofit2.http.Query +import java.time.Instant + +/** + * The Note Board. This is the proposed contract; the backend and the mock server don't serve + * it yet, so every call 404s for now. + */ +interface NoteApiService { + + /** + * Pinned notes, then the rest newest first. Pass the previous page's `nextCursor` as [before] + * to load older notes. [limit] defaults to 30 (max 100). + */ + @GET("houses/{houseId}/notes") + suspend fun getNotes( + @Path("houseId") houseId: Int, + @Query("before") before: Int? = null, + @Query("limit") limit: Int? = null, + ): NoteBoardResponse + + @POST("houses/{houseId}/notes") + suspend fun createNote(@Path("houseId") houseId: Int, @Body body: CreateNoteBody): NoteResponse + + @GET("houses/{houseId}/notes/{noteId}") + suspend fun getNote(@Path("houseId") houseId: Int, @Path("noteId") noteId: Int): NoteResponse + + /** Your own notes only. */ + @PATCH("houses/{houseId}/notes/{noteId}") + suspend fun updateNote( + @Path("houseId") houseId: Int, + @Path("noteId") noteId: Int, + @Body body: UpdateNoteBody, + ): NoteResponse + + /** The author or the house owner. */ + @DELETE("houses/{houseId}/notes/{noteId}") + suspend fun deleteNote(@Path("houseId") houseId: Int, @Path("noteId") noteId: Int) + + /** Pinning again replaces the end time. */ + @POST("houses/{houseId}/notes/{noteId}/pin") + suspend fun pinNote( + @Path("houseId") houseId: Int, + @Path("noteId") noteId: Int, + @Body body: PinNoteBody, + ): NoteResponse + + @DELETE("houses/{houseId}/notes/{noteId}/pin") + suspend fun unpinNote(@Path("houseId") houseId: Int, @Path("noteId") noteId: Int): NoteResponse + + /** Hides the note from you. You can't report your own. */ + @POST("houses/{houseId}/notes/{noteId}/report") + suspend fun reportNote( + @Path("houseId") houseId: Int, + @Path("noteId") noteId: Int, + @Body body: ReportNoteBody, + ) +} + +data class NoteBoardResponse( + /** Only filled on the first page. */ + val pinned: List, + val notes: List, + /** The `before` value for the next page, or null at the end. */ + val nextCursor: Int?, +) + +data class NoteResponse(val note: NoteDto) + +data class CreateNoteBody( + val body: String, + val color: NoteColorDto? = null, + val pinnedUntil: Instant? = null, +) + +data class UpdateNoteBody( + val body: String? = null, + val color: NoteColorDto? = null, +) + +/** Leave [until] null to pin until someone unpins it. */ +data class PinNoteBody(val until: Instant? = null) + +/** Up to 200 characters. */ +data class ReportNoteBody(val reason: String? = null) diff --git a/app/src/main/java/com/example/roomie/data/api/RetrofitInstance.kt b/app/src/main/java/com/example/roomie/data/api/RetrofitInstance.kt index a15e26c..d27bcd2 100644 --- a/app/src/main/java/com/example/roomie/data/api/RetrofitInstance.kt +++ b/app/src/main/java/com/example/roomie/data/api/RetrofitInstance.kt @@ -30,6 +30,7 @@ class RetrofitInstance internal constructor(retrofit: Retrofit) { val houseApi: HouseApiService by lazy { retrofit.create(HouseApiService::class.java) } val choreApi: ChoreApiService by lazy { retrofit.create(ChoreApiService::class.java) } val shoppingApi: ShoppingApiService by lazy { retrofit.create(ShoppingApiService::class.java) } + val noteApi: NoteApiService by lazy { retrofit.create(NoteApiService::class.java) } } /** Builds the client. Separate from [RetrofitInstance] so tests can point it at a fake server. */