Skip to content

Commit

Permalink
added http response handlers for Kelvin
Browse files Browse the repository at this point in the history
  • Loading branch information
benzlokzik committed Nov 6, 2023
1 parent fbff28b commit a670c3d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ pub fn init(cfg: &mut web::ServiceConfig) {
cfg.service(
web::scope("/convert")
.route("/c_to_f/{temp}", web::get().to(temperature::celsius_to_fahrenheit))
.route("/f_to_c/{temp}", web::get().to(temperature::fahrenheit_to_celsius)),
.route("/f_to_c/{temp}", web::get().to(temperature::fahrenheit_to_celsius))
.route("/k_to_c/{temp}", web::get().to(temperature::kelvin_to_celsius))
.route("/c_to_k/{temp}", web::get().to(temperature::celsius_to_kelvin))
.route("/k_to_f/{temp}", web::get().to(temperature::kelvin_to_fahrenheit))
.route("/f_to_k/{temp}", web::get().to(temperature::fahrenheit_to_kelvin)),
);
}
26 changes: 25 additions & 1 deletion src/handlers/temperature.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use actix_web::{web, HttpResponse, Responder};
use crate::models::temperature_conversion::{Celsius, Fahrenheit};
use crate::models::temperature_conversion::{Celsius, Fahrenheit, Kelvin};

pub async fn celsius_to_fahrenheit(temp: web::Path<f64>) -> impl Responder {
let temp = temp.into_inner();
Expand All @@ -12,3 +12,27 @@ pub async fn fahrenheit_to_celsius(temp: web::Path<f64>) -> impl Responder {
let celsius = Fahrenheit::new(temp).to_celsius();
HttpResponse::Ok().json(celsius)
}

pub async fn kelvin_to_celsius(temp: web::Path<f64>) -> impl Responder {
let temp = temp.into_inner();
let celsius = Kelvin::new(temp).to_celsius();
HttpResponse::Ok().json(celsius)
}

pub async fn celsius_to_kelvin(temp: web::Path<f64>) -> impl Responder {
let temp = temp.into_inner();
let kelvin = Celsius::new(temp).to_kelvin();
HttpResponse::Ok().json(kelvin)
}

pub async fn kelvin_to_fahrenheit(temp: web::Path<f64>) -> impl Responder {
let temp = temp.into_inner();
let fahrenheit = Kelvin::new(temp).to_fahrenheit();
HttpResponse::Ok().json(fahrenheit)
}

pub async fn fahrenheit_to_kelvin(temp: web::Path<f64>) -> impl Responder {
let temp = temp.into_inner();
let kelvin = Fahrenheit::new(temp).to_kelvin();
HttpResponse::Ok().json(kelvin)
}

0 comments on commit a670c3d

Please sign in to comment.