From d9d71dfeb956a150e27d9c9ebcecb589e8571bd9 Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Sat, 26 Oct 2024 00:46:18 +0200 Subject: [PATCH] Exercise 07-10 --- exercises/07_threads/10_patch/src/lib.rs | 26 +++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/exercises/07_threads/10_patch/src/lib.rs b/exercises/07_threads/10_patch/src/lib.rs index 492f767..61bdd43 100644 --- a/exercises/07_threads/10_patch/src/lib.rs +++ b/exercises/07_threads/10_patch/src/lib.rs @@ -35,7 +35,16 @@ impl TicketStoreClient { Ok(response_receiver.recv().unwrap()) } - pub fn update(&self, ticket_patch: TicketPatch) -> Result<(), OverloadedError> {} + pub fn update(&self, ticket_patch: TicketPatch) -> Result<(), OverloadedError> { + let (response_sender, response_receiver) = sync_channel(1); + self.sender + .try_send(Command::Update { + patch: ticket_patch, + response_channel: response_sender, + }) + .map_err(|_| OverloadedError)?; + Ok(response_receiver.recv().unwrap()) + } } #[derive(Debug, thiserror::Error)] @@ -63,7 +72,7 @@ enum Command { }, } -pub fn server(receiver: Receiver) { +fn server(receiver: Receiver) { let mut store = TicketStore::new(); loop { match receiver.recv() { @@ -85,7 +94,18 @@ pub fn server(receiver: Receiver) { patch, response_channel, }) => { - todo!() + if let Some(ticket) = store.get_mut(patch.id) { + if let Some(title) = patch.title { + ticket.title = title; + } + if let Some(description) = patch.description { + ticket.description = description; + } + if let Some(status) = patch.status { + ticket.status = status; + } + } + let _ = response_channel.send(()); } Err(_) => { // There are no more senders, so we can safely break -- 2.45.2