rpa_derives/database/rpa/delete.rs
1/**
2 Rpa (Rust Persistence API) Derive Delete Partial implementation.
3 Copyright (C) 2019 Jonathan Franco
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>.
17**/
18
19use quote::quote;
20
21pub fn get_impl(type_name: &syn::Ident, table_name: &syn::Ident) -> proc_macro2::TokenStream {
22 quote! {
23 fn delete(entity_id: &String, connection: &DatabaseConnection) -> Result<usize, RpaError> {
24 let error_message: String = format!("A problem has occurred deleting {} with id {}", stringify!(#type_name), entity_id);
25 let result = diesel::delete(#table_name::table.find(entity_id)).execute(connection);
26 RpaError::map_result::<usize>(error_message, result)
27 }
28 fn delete_self(self: Self, connection: &DatabaseConnection) -> Result<usize, RpaError> {
29 let error_message: String = format!("A problem has occurred updating {} with id {}", stringify!(#type_name), &self.id);
30 let result = diesel::delete(#table_name::table.find(&self.id)).execute(connection);
31 RpaError::map_result::<usize>(error_message, result)
32 }
33 }
34}