- update: major refactor of the database-service to make it easy to add newer api services
- add: character database api
This commit is contained in:
93
database-service/src/grpc/character_service.rs
Normal file
93
database-service/src/grpc/character_service.rs
Normal file
@@ -0,0 +1,93 @@
|
||||
use crate::grpc::{CharacterRequest, CharacterResponse, CreateCharacterRequest, DeleteCharacterRequest, Empty};
|
||||
use crate::grpc::character_service_server::CharacterService;
|
||||
use crate::grpc::database_service::MyDatabaseService;
|
||||
use tonic::{Request, Response, Status};
|
||||
|
||||
#[tonic::async_trait]
|
||||
impl CharacterService for MyDatabaseService {
|
||||
async fn get_character(
|
||||
&self,
|
||||
request: Request<CharacterRequest>,
|
||||
) -> Result<Response<CharacterResponse>, Status> {
|
||||
let req = request.into_inner();
|
||||
let repo = &self.db.character_repo;
|
||||
|
||||
let character = repo
|
||||
.get_character_by_id(req.character_id)
|
||||
.await
|
||||
.map_err(|_| Status::not_found("Character not found"))?;
|
||||
|
||||
let response = CharacterResponse {
|
||||
id: character.id,
|
||||
user_id: character.user_id,
|
||||
name: character.name,
|
||||
level: character.level as i32,
|
||||
experience: character.experience,
|
||||
inventory: character.inventory.to_string(),
|
||||
stats: character.stats.to_string(),
|
||||
looks: character.looks.to_string(),
|
||||
position: character.position.to_string(),
|
||||
created_at: character.created_at.to_string(),
|
||||
updated_at: character.updated_at.to_string(),
|
||||
is_active: character.is_active,
|
||||
};
|
||||
|
||||
Ok(Response::new(response))
|
||||
}
|
||||
|
||||
async fn create_character(
|
||||
&self,
|
||||
request: Request<CreateCharacterRequest>,
|
||||
) -> Result<Response<CharacterResponse>, Status> {
|
||||
let req = request.into_inner();
|
||||
let repo = &self.db.character_repo;
|
||||
|
||||
let character_id = repo
|
||||
.create_character(
|
||||
req.user_id,
|
||||
&req.name,
|
||||
serde_json::from_str(&req.inventory).unwrap_or_default(),
|
||||
serde_json::from_str(&req.stats).unwrap_or_default(),
|
||||
serde_json::from_str(&req.looks).unwrap_or_default(),
|
||||
serde_json::from_str(&req.position).unwrap_or_default(),
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Status::internal("Failed to create character"))?;
|
||||
|
||||
let character = repo
|
||||
.get_character_by_id(character_id)
|
||||
.await
|
||||
.map_err(|_| Status::not_found("Character not found"))?;
|
||||
|
||||
let response = CharacterResponse {
|
||||
id: character.id,
|
||||
user_id: character.user_id,
|
||||
name: character.name,
|
||||
level: character.level as i32,
|
||||
experience: character.experience,
|
||||
inventory: character.inventory.to_string(),
|
||||
stats: character.stats.to_string(),
|
||||
looks: character.looks.to_string(),
|
||||
position: character.position.to_string(),
|
||||
created_at: character.created_at.to_string(),
|
||||
updated_at: character.updated_at.to_string(),
|
||||
is_active: character.is_active,
|
||||
};
|
||||
|
||||
Ok(Response::new(response))
|
||||
}
|
||||
|
||||
async fn delete_character(
|
||||
&self,
|
||||
request: Request<DeleteCharacterRequest>,
|
||||
) -> Result<Response<Empty>, Status> {
|
||||
let req = request.into_inner();
|
||||
let repo = &self.db.character_repo;
|
||||
|
||||
repo.delete_character(req.character_id)
|
||||
.await
|
||||
.map_err(|_| Status::internal("Failed to delete character"))?;
|
||||
|
||||
Ok(Response::new(Empty {}))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user