li7y/src/manage/item.rs

35 lines
1.1 KiB
Rust
Raw Normal View History

2024-06-24 22:46:04 +02:00
// SPDX-FileCopyrightText: 2024 Simon Bruder <simon@sbruder.de>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
use diesel::pg::PgConnection;
use diesel::prelude::*;
use uuid::Uuid;
use crate::{models::*, schema};
pub fn add(conn: &mut PgConnection, new_item: NewItem) -> Result<Item, diesel::result::Error> {
diesel::insert_into(schema::items::table)
.values(new_item)
.returning(Item::as_returning())
.get_result(conn)
}
pub fn get(conn: &mut PgConnection, id: Uuid) -> Result<Item, diesel::result::Error> {
schema::items::table
.filter(schema::items::id.eq(id))
.select(Item::as_select())
.first(conn)
}
pub fn get_all(conn: &mut PgConnection) -> Result<Vec<Item>, diesel::result::Error> {
schema::items::table.select(Item::as_select()).load(conn)
}
2024-07-03 18:47:29 +02:00
pub fn update(conn: &mut PgConnection, id: Uuid, modified_item: NewItem) -> Result<Item, diesel::result::Error> {
diesel::update(schema::items::table.filter(schema::items::id.eq(id)))
.set(modified_item)
.returning(Item::as_returning())
.get_result(conn)
}