use http::Method;
use serde::Serialize;
use super::Term;
use crate::{api, auth::Authenticated, query::DefaultModel, Endpoint};
#[derive(Clone, Debug, Eq, Ord, Hash, PartialEq, PartialOrd)]
pub struct CreateTerm {
pub term: String,
pub project: api::ProjectId,
}
impl CreateTerm {
pub fn new(term: impl Into<String>, project: api::ProjectId) -> Self {
Self {
term: term.into(),
project,
}
}
}
impl Endpoint for CreateTerm {
type AccessControl = Authenticated;
fn method(&self) -> Method {
Method::POST
}
fn endpoint(&self) -> std::borrow::Cow<'static, str> {
format!("projects/{}/terms", self.project).into()
}
fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>, crate::BodyError> {
#[derive(Serialize)]
struct Dto<'a> {
pub value: &'a str,
}
let dto = Dto { value: &self.term };
Ok(Some((
api::mime_types::JSON,
serde_json::to_string(&dto)?.into_bytes(),
)))
}
}
impl DefaultModel for CreateTerm {
type Model = Term;
}