+use data::{Ticket, TicketDraft};
use std::sync::mpsc::{Receiver, Sender};
+use store::TicketId;
+
use crate::store::TicketStore;
pub mod data;
// 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> {
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;
+ }
}
}
}