Exercise 07-07
authorGreg Burri <greg.burri@gmail.com>
Thu, 24 Oct 2024 14:16:23 +0000 (16:16 +0200)
committerGreg Burri <greg.burri@gmail.com>
Thu, 24 Oct 2024 14:16:23 +0000 (16:16 +0200)
exercises/07_threads/07_ack/src/lib.rs

index 746c355..9eb02dc 100644 (file)
@@ -1,4 +1,7 @@
+use data::{Ticket, TicketDraft};
 use std::sync::mpsc::{Receiver, Sender};
+use store::TicketId;
+
 use crate::store::TicketStore;
 
 pub mod data;
@@ -6,8 +9,14 @@ pub mod store;
 
 // Refer to the tests to understand the expected schema.
 pub enum Command {
-    Insert { todo!() },
-    Get { todo!() }
+    Insert {
+        draft: TicketDraft,
+        response_sender: Sender<TicketId>,
+    },
+    Get {
+        id: TicketId,
+        response_sender: Sender<Option<Ticket>>,
+    },
 }
 
 pub fn launch() -> Sender<Command> {
@@ -21,19 +30,29 @@ pub fn server(receiver: Receiver<Command>) {
     let mut store = TicketStore::new();
     loop {
         match receiver.recv() {
-            Ok(Command::Insert {}) => {
-                todo!()
+            Ok(Command::Insert {
+                draft,
+                response_sender,
+            }) => {
+                let ticket_id = store.add_ticket(draft);
+                response_sender
+                    .send(ticket_id)
+                    .expect("Cannot send response");
             }
             Ok(Command::Get {
-                todo!()
+                id,
+                response_sender,
             }) => {
-                todo!()
+                let ticket = store.get(id);
+                response_sender
+                    .send(ticket.cloned())
+                    .expect("Cannot send response");
             }
             Err(_) => {
                 // There are no more senders, so we can safely break
                 // and shut down the server.
-                break
-            },
+                break;
+            }
         }
     }
 }