1+ use crate :: { models:: students_model:: Students , models:: user_model:: User , repository:: mongodb_repo:: MongoRepo } ;
2+ use mongodb:: { bson:: oid:: ObjectId , results:: InsertOneResult } ;
3+ use rocket:: { http:: Status , serde:: json:: Json , State } ;
4+ use bcrypt:: { hash, DEFAULT_COST } ;
5+ use bcrypt:: verify;
6+ use jsonwebtoken:: { encode, EncodingKey , Header , Algorithm } ;
7+ use std:: collections:: BTreeMap ;
8+ use rocket:: serde:: Serialize ;
9+ use rocket:: serde:: Deserialize ;
10+ use chrono:: { Utc , Duration } ;
11+ use std:: env;
12+ use dotenv:: dotenv;
13+ use mongodb:: bson:: DateTime as BsonDateTime ;
14+
15+ #[ post( "/student" , data = "<new_students>" ) ]
16+ pub fn create_students (
17+ db : & State < MongoRepo > ,
18+ new_students : Json < Students > ,
19+ ) -> Result < Json < InsertOneResult > , Status > {
20+ let data = Students {
21+ id : None ,
22+ duree : new_students. duree ,
23+ niveau : new_students. niveau . to_owned ( ) ,
24+ type_contrat : new_students. type_contrat . to_owned ( ) ,
25+ date_debut : new_students. date_debut ,
26+ lieu : new_students. lieu . to_owned ( ) ,
27+ recherche : new_students. recherche ,
28+
29+ } ;
30+ let students_detail = db. create_students ( data) ;
31+ match students_detail {
32+ Ok ( students) => Ok ( Json ( students) ) ,
33+ Err ( _) => Err ( Status :: InternalServerError ) ,
34+ }
35+ }
36+
37+
38+ #[ get( "/student/<path>" ) ]
39+ pub fn get_students ( db : & State < MongoRepo > , path : String ) -> Result < Json < Students > , Status > {
40+ let id = path;
41+ if id. is_empty ( ) {
42+ return Err ( Status :: BadRequest ) ;
43+ } ;
44+ let students_detail = db. get_students ( & id) ;
45+ match students_detail {
46+ Ok ( students) => Ok ( Json ( students) ) ,
47+ Err ( _) => Err ( Status :: InternalServerError ) ,
48+ }
49+ }
50+
51+ #[ put( "/student/<path>" , data = "<new_students>" ) ]
52+ pub fn update_students (
53+ db : & State < MongoRepo > ,
54+ path : String ,
55+ new_students : Json < Students > ,
56+ ) -> Result < Json < Students > , Status > {
57+ let id = path;
58+ if id. is_empty ( ) {
59+ return Err ( Status :: BadRequest ) ;
60+ } ;
61+ let data = Students {
62+ id : Some ( ObjectId :: parse_str ( & id) . unwrap ( ) ) ,
63+ duree : new_students. duree ,
64+ niveau : new_students. niveau . to_owned ( ) ,
65+ type_contrat : new_students. type_contrat . to_owned ( ) ,
66+ date_debut : new_students. date_debut ,
67+ lieu : new_students. lieu . to_owned ( ) ,
68+ recherche : new_students. recherche ,
69+ } ;
70+ let update_result = db. update_students ( & id, data) ;
71+ match update_result {
72+ Ok ( update) => {
73+ if update. matched_count == 1 {
74+ let updated_students_info = db. get_students ( & id) ;
75+ return match updated_students_info {
76+ Ok ( students) => Ok ( Json ( students) ) ,
77+ Err ( _) => Err ( Status :: InternalServerError ) ,
78+ } ;
79+ } else {
80+ return Err ( Status :: NotFound ) ;
81+ }
82+ }
83+ Err ( _) => Err ( Status :: InternalServerError ) ,
84+ }
85+ }
86+
87+ #[ delete( "/student/<path>" ) ]
88+ pub fn delete_students ( db : & State < MongoRepo > , path : String ) -> Result < Json < & str > , Status > {
89+ let id = path;
90+ if id. is_empty ( ) {
91+ return Err ( Status :: BadRequest ) ;
92+ } ;
93+ let result = db. delete_students ( & id) ;
94+ match result {
95+ Ok ( res) => {
96+ if res. deleted_count == 1 {
97+ return Ok ( Json ( "Students successfully deleted!" ) ) ;
98+ } else {
99+ return Err ( Status :: NotFound ) ;
100+ }
101+ }
102+ Err ( _) => Err ( Status :: InternalServerError ) ,
103+ }
104+ }
105+
106+ #[ get( "/students" ) ]
107+ pub fn get_all_students ( db : & State < MongoRepo > ) -> Result < Json < Vec < Students > > , Status > {
108+ let students = db. get_all_students ( ) ;
109+ match students {
110+ Ok ( students) => Ok ( Json ( students) ) ,
111+ Err ( _) => Err ( Status :: InternalServerError ) ,
112+ }
113+ }
0 commit comments