diff --git a/shared/src/main/scala/cs214/webapp/Messages.scala b/shared/src/main/scala/cs214/webapp/Messages.scala index 7457ee2e395c31f8d79041bcab7394747501a7b2..46954f3ac88c3e5d4ea1083bac18b675dd4bd7b1 100644 --- a/shared/src/main/scala/cs214/webapp/Messages.scala +++ b/shared/src/main/scala/cs214/webapp/Messages.scala @@ -49,27 +49,24 @@ val SocketResponseWire = TryWire(IdentityWire) /** A response to the list-apps query */ case class ListAppsResponse(apps: Seq[AppInfo]) +object AppInfoWire extends WireFormat[AppInfo]: + def encode(t: AppInfo): ujson.Value = t match + case AppInfo(id, name, description, year) => + Obj("id" -> id, "name" -> name, "description" -> description, "year" -> year) + def decode(js: ujson.Value): Try[AppInfo] = Try: + AppInfo( + id = js("id").str, + name = js("name").str, + description = js("description").str, + year = js("year").num.toInt + ) + object ListAppsResponse: object Wire extends WireFormat[ListAppsResponse]: def encode(t: ListAppsResponse): ujson.Value = - ujson.Obj("apps" -> Arr(t.apps.map { - case AppInfo(id, name, description, year) => - Obj("id" -> id, "name" -> name, "desc" -> description, "year" -> year) - }*)) + Obj("apps" -> Arr(t.apps.map(AppInfoWire.encode)*)) def decode(js: ujson.Value): Try[ListAppsResponse] = Try: - ListAppsResponse( - js("apps").arr - .map(_.obj) - .map(js => - AppInfo( - id = js("id").str, - name = js("name").str, - description = js("desc").str, - year = js("year").num.toInt - ) - ) - .to(Seq) - ) + ListAppsResponse(apps = js("apps").arr.map(AppInfoWire.decode(_).get).to(Seq)) /** An HTTP request sent to create a new application instance for the server */ case class CreateInstanceRequest(appName: String, userIds: Seq[UserId])