|  | 
|  | 1 | +# api_code/api/crud.py | 
|  | 2 | +from datetime import datetime, timezone | 
|  | 3 | + | 
|  | 4 | +from sqlalchemy import delete, update | 
|  | 5 | +from sqlalchemy.orm import Session, aliased | 
|  | 6 | + | 
|  | 7 | +from . import models, schemas | 
|  | 8 | + | 
|  | 9 | +# USERS | 
|  | 10 | + | 
|  | 11 | + | 
|  | 12 | +def get_users(db: Session, email: str = None): | 
|  | 13 | +    q = db.query(models.User) | 
|  | 14 | +    if email is not None: | 
|  | 15 | +        q = q.filter(models.User.email.ilike(f"%{email}%")) | 
|  | 16 | +    return q.all() | 
|  | 17 | + | 
|  | 18 | + | 
|  | 19 | +def get_user(db: Session, user_id: int): | 
|  | 20 | +    return ( | 
|  | 21 | +        db.query(models.User) | 
|  | 22 | +        .filter(models.User.id == user_id) | 
|  | 23 | +        .first() | 
|  | 24 | +    ) | 
|  | 25 | + | 
|  | 26 | + | 
|  | 27 | +def get_user_by_email(db: Session, email: str): | 
|  | 28 | +    return ( | 
|  | 29 | +        db.query(models.User) | 
|  | 30 | +        .filter(models.User.email.ilike(email)) | 
|  | 31 | +        .first() | 
|  | 32 | +    ) | 
|  | 33 | + | 
|  | 34 | + | 
|  | 35 | +def create_user( | 
|  | 36 | +    db: Session, user: schemas.UserCreate, user_id: int = None | 
|  | 37 | +): | 
|  | 38 | +    hashed_password = models.User.hash_password(user.password) | 
|  | 39 | +    user_dict = { | 
|  | 40 | +        **user.dict(exclude_unset=True), | 
|  | 41 | +        "password": hashed_password, | 
|  | 42 | +    } | 
|  | 43 | +    if user_id is not None: | 
|  | 44 | +        user_dict.update({"id": user_id}) | 
|  | 45 | +    db_user = models.User(**user_dict) | 
|  | 46 | +    db.add(db_user) | 
|  | 47 | +    db.commit() | 
|  | 48 | +    db.refresh(db_user) | 
|  | 49 | +    return db_user | 
|  | 50 | + | 
|  | 51 | + | 
|  | 52 | +def update_user( | 
|  | 53 | +    db: Session, user: schemas.UserUpdate, user_id: int | 
|  | 54 | +): | 
|  | 55 | +    user_dict = { | 
|  | 56 | +        **user.dict(exclude_unset=True), | 
|  | 57 | +    } | 
|  | 58 | +    if user.password is not None: | 
|  | 59 | +        user_dict.update( | 
|  | 60 | +            {"password": models.User.hash_password(user.password)} | 
|  | 61 | +        ) | 
|  | 62 | +    stm = ( | 
|  | 63 | +        update(models.User) | 
|  | 64 | +        .where(models.User.id == user_id) | 
|  | 65 | +        .values(user_dict) | 
|  | 66 | +    ) | 
|  | 67 | +    result = db.execute(stm) | 
|  | 68 | +    db.commit() | 
|  | 69 | +    return result.rowcount | 
|  | 70 | + | 
|  | 71 | + | 
|  | 72 | +def delete_user(db: Session, user_id: int): | 
|  | 73 | +    stm = delete(models.User).where(models.User.id == user_id) | 
|  | 74 | +    result = db.execute(stm) | 
|  | 75 | +    db.commit() | 
|  | 76 | +    return result.rowcount | 
|  | 77 | + | 
|  | 78 | + | 
|  | 79 | +# STATIONS | 
|  | 80 | + | 
|  | 81 | + | 
|  | 82 | +def get_stations(db: Session, code: str = None): | 
|  | 83 | +    q = db.query(models.Station) | 
|  | 84 | +    if code is not None: | 
|  | 85 | +        q = q.filter(models.Station.code.ilike(code)) | 
|  | 86 | +    return q.all() | 
|  | 87 | + | 
|  | 88 | + | 
|  | 89 | +def get_station(db: Session, station_id: int): | 
|  | 90 | +    return ( | 
|  | 91 | +        db.query(models.Station) | 
|  | 92 | +        .filter(models.Station.id == station_id) | 
|  | 93 | +        .first() | 
|  | 94 | +    ) | 
|  | 95 | + | 
|  | 96 | + | 
|  | 97 | +def get_station_by_code(db: Session, code: str): | 
|  | 98 | +    return ( | 
|  | 99 | +        db.query(models.Station) | 
|  | 100 | +        .filter(models.Station.code.ilike(code)) | 
|  | 101 | +        .first() | 
|  | 102 | +    ) | 
|  | 103 | + | 
|  | 104 | + | 
|  | 105 | +def create_station( | 
|  | 106 | +    db: Session, | 
|  | 107 | +    station: schemas.StationCreate, | 
|  | 108 | +): | 
|  | 109 | +    db_station = models.Station(**station.dict()) | 
|  | 110 | +    db.add(db_station) | 
|  | 111 | +    db.commit() | 
|  | 112 | +    db.refresh(db_station) | 
|  | 113 | +    return db_station | 
|  | 114 | + | 
|  | 115 | + | 
|  | 116 | +def update_station( | 
|  | 117 | +    db: Session, station: schemas.StationUpdate, station_id: int | 
|  | 118 | +): | 
|  | 119 | +    stm = ( | 
|  | 120 | +        update(models.Station) | 
|  | 121 | +        .where(models.Station.id == station_id) | 
|  | 122 | +        .values(station.dict(exclude_unset=True)) | 
|  | 123 | +    ) | 
|  | 124 | +    result = db.execute(stm) | 
|  | 125 | +    db.commit() | 
|  | 126 | +    return result.rowcount | 
|  | 127 | + | 
|  | 128 | + | 
|  | 129 | +def delete_station(db: Session, station_id: int): | 
|  | 130 | +    stm = delete(models.Station).where( | 
|  | 131 | +        models.Station.id == station_id | 
|  | 132 | +    ) | 
|  | 133 | +    result = db.execute(stm) | 
|  | 134 | +    db.commit() | 
|  | 135 | +    return result.rowcount | 
|  | 136 | + | 
|  | 137 | + | 
|  | 138 | +# TRAINS | 
|  | 139 | + | 
|  | 140 | + | 
|  | 141 | +def get_trains( | 
|  | 142 | +    db: Session, | 
|  | 143 | +    station_from_code: str = None, | 
|  | 144 | +    station_to_code: str = None, | 
|  | 145 | +    include_all: bool = False, | 
|  | 146 | +): | 
|  | 147 | +    q = db.query(models.Train) | 
|  | 148 | + | 
|  | 149 | +    if station_from_code is not None: | 
|  | 150 | +        st_from = aliased(models.Station) | 
|  | 151 | +        q = q.join(st_from, models.Train.station_from) | 
|  | 152 | +        q = q.filter(st_from.code.ilike(f"%{station_from_code}%")) | 
|  | 153 | + | 
|  | 154 | +    if station_to_code is not None: | 
|  | 155 | +        st_to = aliased(models.Station) | 
|  | 156 | +        q = q.join(st_to, models.Train.station_to) | 
|  | 157 | +        q = q.filter(st_to.code.ilike(f"%{station_to_code}%")) | 
|  | 158 | + | 
|  | 159 | +    if not include_all: | 
|  | 160 | +        now = datetime.now(tz=timezone.utc) | 
|  | 161 | +        q = q.filter(models.Train.departs_at > now) | 
|  | 162 | + | 
|  | 163 | +    return q.all() | 
|  | 164 | + | 
|  | 165 | + | 
|  | 166 | +def get_train(db: Session, train_id: int): | 
|  | 167 | +    return ( | 
|  | 168 | +        db.query(models.Train) | 
|  | 169 | +        .filter(models.Train.id == train_id) | 
|  | 170 | +        .first() | 
|  | 171 | +    ) | 
|  | 172 | + | 
|  | 173 | + | 
|  | 174 | +def get_train_by_name(db: Session, name: str): | 
|  | 175 | +    return ( | 
|  | 176 | +        db.query(models.Train) | 
|  | 177 | +        .filter(models.Train.name == name) | 
|  | 178 | +        .first() | 
|  | 179 | +    ) | 
|  | 180 | + | 
|  | 181 | + | 
|  | 182 | +def create_train(db: Session, train: schemas.TrainCreate): | 
|  | 183 | +    train_dict = train.dict(exclude_unset=True) | 
|  | 184 | +    db_train = models.Train(**train_dict) | 
|  | 185 | +    db.add(db_train) | 
|  | 186 | +    db.commit() | 
|  | 187 | +    db.refresh(db_train) | 
|  | 188 | +    return db_train | 
|  | 189 | + | 
|  | 190 | + | 
|  | 191 | +def delete_train(db: Session, train_id: int): | 
|  | 192 | +    stm = delete(models.Train).where(models.Train.id == train_id) | 
|  | 193 | +    result = db.execute(stm) | 
|  | 194 | +    db.commit() | 
|  | 195 | +    return result.rowcount | 
|  | 196 | + | 
|  | 197 | + | 
|  | 198 | +# TICKETS | 
|  | 199 | + | 
|  | 200 | + | 
|  | 201 | +def get_tickets(db: Session): | 
|  | 202 | +    return db.query(models.Ticket).all() | 
|  | 203 | + | 
|  | 204 | + | 
|  | 205 | +def get_ticket(db: Session, ticket_id: int): | 
|  | 206 | +    return ( | 
|  | 207 | +        db.query(models.Ticket) | 
|  | 208 | +        .filter(models.Ticket.id == ticket_id) | 
|  | 209 | +        .first() | 
|  | 210 | +    ) | 
|  | 211 | + | 
|  | 212 | + | 
|  | 213 | +def create_ticket(db: Session, ticket: schemas.TicketCreate): | 
|  | 214 | +    ticket_dict = ticket.dict(exclude_unset=True) | 
|  | 215 | +    ticket_dict.update( | 
|  | 216 | +        {"created_at": datetime.now(tz=timezone.utc)} | 
|  | 217 | +    ) | 
|  | 218 | +    db_ticket = models.Ticket(**ticket_dict) | 
|  | 219 | +    db.add(db_ticket) | 
|  | 220 | +    db.commit() | 
|  | 221 | +    db.refresh(db_ticket) | 
|  | 222 | +    return db_ticket | 
|  | 223 | + | 
|  | 224 | + | 
|  | 225 | +def delete_ticket(db: Session, ticket_id: int): | 
|  | 226 | +    stm = delete(models.Ticket).where( | 
|  | 227 | +        models.Ticket.id == ticket_id | 
|  | 228 | +    ) | 
|  | 229 | +    result = db.execute(stm) | 
|  | 230 | +    db.commit() | 
|  | 231 | +    return result.rowcount | 
0 commit comments