Skip to content

Commit

Permalink
Annuler Réservation
Browse files Browse the repository at this point in the history
  • Loading branch information
e.elhjoujy committed Oct 23, 2023
1 parent 77df404 commit d53681a
Show file tree
Hide file tree
Showing 27 changed files with 464 additions and 118 deletions.
15 changes: 15 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ services:
networks:
- db
restart: unless-stopped
airafraik_test:
image: postgres
environment:
POSTGRES_USER: ${USER}
POSTGRES_PASSWORD: ${PASSWORD}
POSTGRES_DB: ${DATABASE}
PGUSER: ${USER}
PGDATA: /data/postgres
volumes:
- db:/data/postgres
ports:
- "5331:5431"
networks:
- db
restart: unless-stopped

networks:
db:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public static void confirmationReservation(){
//
//TODO : CONFIRMATION
if (false){
reservationEntity.setStatus(ReservationStatus.CONFIRMED);
reservationEntity.setStatus(ReservationStatus.CONFIRMER);

}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ma/yc/airafraik/core/EmailProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected PasswordAuthentication getPasswordAuthentication() {
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email));
message.setSubject(subject);
message.setText("Here is the code of verification authentic: "+body);
message.setText(body);
Transport.send(message);
return true;
} catch (MessagingException e) {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/ma/yc/airafraik/core/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ public static VolEntity getVolEntity() {
return vol;
}

public static Date addHours(int i) {
Print.log("i : "+i);
Date date =new Date(new Date().getTime() + i * 60 * 60 * 1000);
return date;

}

//add more helper method
//todo : work with local storage to store information
public boolean storeInformation(String key , String value){
Expand Down
28 changes: 26 additions & 2 deletions src/main/java/ma/yc/airafraik/dao/Impl/ReservationDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ public Collection<ReservationEntity> consulterReservations() {

@Override
public Collection<ReservationEntity> consulterReservations(String code) {
return null;
Query query = entityManager.createNativeQuery("SELECT * FROM reservation where code = :code",ReservationEntity.class);
query.setParameter("code",code);
return (Collection<ReservationEntity>) query.getResultList();
}


Expand All @@ -78,7 +80,7 @@ public ArrayList<ReservationEntity> AfficheLesStatistiques(HashMap<String, Strin
Query query = entityManager.createNativeQuery("SELECT * FROM reservation WHERE date_reservation BETWEEN :date_1 AND :date_2 AND status = :etat_reservation", ReservationEntity.class);
query.setParameter("date_1", conditions.get("date_1"));
query.setParameter("date_2", conditions.get("date_2"));
query.setParameter("etat_reservation", ReservationStatus.CONFIRMED.toString());
query.setParameter("etat_reservation", ReservationStatus.CONFIRMER.toString());
ArrayList<ReservationEntity> reservationEntities = (ArrayList<ReservationEntity>) query.getResultList();

return reservationEntities;
Expand All @@ -93,4 +95,26 @@ public ArrayList<ReservationEntity> AfficheLesStatistiques(HashMap<String, Strin
ArrayList<ReservationEntity> reservationEntities = (ArrayList<ReservationEntity>) query.getResultList();
return reservationEntities;
}

@Override
public int statisticsReservation(ReservationStatus reservationStatus, String mois) {
this.entityManager.createNativeQuery("SELECT count(*) FROM reservation WHERE status = :status AND date_reservation LIKE :mois")
.setParameter("status",reservationStatus.toString())
.setParameter("mois",mois)
.getSingleResult();
return 0;
}

@Override
public boolean annulerReservation(ReservationEntity reservationEntity) {
if (reservationEntity == null){
return false;
}

this.transaction.begin();
ReservationEntity reservationEntity1 = entityManager.find(ReservationEntity.class, reservationEntity.getId());
reservationEntity1.setStatus(ReservationStatus.ANNULER);
this.transaction.commit();
return false;
}
}
24 changes: 24 additions & 0 deletions src/main/java/ma/yc/airafraik/dao/Impl/VolDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,5 +236,29 @@ public Collection<VolEntity> consulterVols() {
return volEntities;
}

@Override
public boolean deleteVol(String idVol) {
Integer id = Integer.parseInt(idVol);
String jpql = "DELETE FROM VolEntity v WHERE v.id = :code";
Query query = entityManager.createQuery(jpql);
query.setParameter("code", id);
try {
transaction.begin();
if (query.executeUpdate() > 0) {
Print.log("Vol supprimé avec succès");
transaction.commit();
return true;
}
Print.log("Erreur lors de la suppression du vol");
transaction.rollback();
}catch (Exception e){
if (transaction != null && transaction.isActive()) {
transaction.rollback(); // Rollback the transaction in case of an exception
}

}
return false;
}


}
6 changes: 2 additions & 4 deletions src/main/java/ma/yc/airafraik/dao/ReservationDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ public interface ReservationDao {
public ArrayList<ReservationEntity> AfficheLesStatistiques(HashMap<String , String> conditions, ReservationStatus reservationStatus);
public ArrayList<ReservationEntity> lesRservationConfirmésPendantMoisOuAns(HashMap<String , String> conditions);
public ArrayList<ReservationEntity> lesRservationAnnulésPendantMoisOuAns(HashMap<String , String> conditions);
int statisticsReservation(ReservationStatus reservationStatus, String mois);





boolean annulerReservation(ReservationEntity reservationEntity);
}
2 changes: 2 additions & 0 deletions src/main/java/ma/yc/airafraik/dao/VolDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ public interface VolDao {
public VolEntity consulterVol(String id);
public ArrayList<VolEntity> consulterVols(HashMap<String, String> conditions);
public Collection<VolEntity> consulterVols();

boolean deleteVol(String idVol);
}
18 changes: 18 additions & 0 deletions src/main/java/ma/yc/airafraik/entities/BagageEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ma.yc.airafraik.entities;

import jakarta.persistence.*;

@Entity
@Table(name = "bagage")
public class BagageEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id ;

private double poids ;
@ManyToOne
private ReservationEntity reservation ;


}
2 changes: 1 addition & 1 deletion src/main/java/ma/yc/airafraik/enums/ReservationStatus.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ma.yc.airafraik.enums;

public enum ReservationStatus {
CONFIRMED,
CONFIRMER,
EN_ATTENTE,
ANNULER ,
EXPIRE
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package ma.yc.airafraik.service;

import ma.yc.airafraik.entities.ReservationEntity;
import ma.yc.airafraik.enums.ReservationStatus;

import java.util.HashMap;

public interface ReservationService {
public double confirmationReservation(ReservationEntity reservationEntity);
public double annulationReservation(ReservationEntity reservationEntity);
public ReservationEntity annulationReservation(ReservationEntity reservationEntity);
public boolean annulationReservation(String numeroReservation);
public boolean envoyerEmail(ReservationEntity reservationEntity);

int statisticsReservation(ReservationStatus reservationStatus, HashMap<String, String> conditions);
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package ma.yc.airafraik.service;

import ma.yc.airafraik.entities.ReservationEntity;
import ma.yc.airafraik.entities.VolEntity;

import java.util.Collection;

public interface SearchVolsService {
public interface VolsService {


public Collection<VolEntity> searchVols(String villeDepart, String villeArrivee, String dateDepart, String dateArrivee, String nombrePassagers);

VolEntity searchVolParId(String id);

Collection<VolEntity> consulterVols();

boolean deleteVol(String idVol);
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
package ma.yc.airafraik.service.impl;

import ma.yc.airafraik.FunctionalTest.Client.ConfirmationReservationEtEnvoyeEmail;
import ma.yc.airafraik.core.EmailProvider;
import ma.yc.airafraik.core.Print;
import ma.yc.airafraik.core.Util;
import ma.yc.airafraik.dao.Impl.ReservationDaoImpl;
import ma.yc.airafraik.dao.ReservationDao;
import ma.yc.airafraik.entities.ReservationEntity;
import ma.yc.airafraik.entities.VolEntity;
import ma.yc.airafraik.enums.ReservationStatus;
import ma.yc.airafraik.enums.ReserveType;
import ma.yc.airafraik.service.PaiementService;
import ma.yc.airafraik.service.ReservationService;

import java.awt.font.TextHitInfo;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

public class ReservationServiceImpl implements ReservationService {

private PaiementService paiementService ;
Expand Down Expand Up @@ -59,24 +68,105 @@ public double confirmationReservation(ReservationEntity reservationEntity) {
return 0;
}else {
//TODO : SEEND EMAIL TO CLIENT WITH HIS RESERVATION CODE AND PDF FILE
this.envoyerEmail(reservationEntity);
if (this.envoyerEmail(reservationEntity))
{
Print.log("Email sent successfully");
}else {
Print.log("Email not sent");
throw new RuntimeException("Email not sent");
}

}

return totalCost;
}

@Override
public double annulationReservation(ReservationEntity reservationEntity) {
public ReservationEntity annulationReservation(ReservationEntity reservationEntity) {
//TODO : annuler sa réservation après la confirmation de son vol dans le cas d'annulation:
// 8% des frais est retenue pour la société AirAfrika > 24h avant le vol

return 1;
return null;
}

@Override
public boolean envoyerEmail(ReservationEntity reservationEntity) {
public boolean annulationReservation(String numeroReservation) {

if (numeroReservation == null){
return false;
}
//TODO : annuler sa réservation après la confirmation de son vol dans le cas d'annulation:
// 8% des frais est retenue pour la société AirAfrika > 24h avant le vol

Collection<ReservationEntity> reservationEntities = this.reservationDao.consulterReservations(numeroReservation);
if (reservationEntities == null){
return false;
}
for (ReservationEntity reservationEntity : reservationEntities){
if (reservationEntity == null){
return false;
}
if (reservationEntity.getStatus() == ReservationStatus.CONFIRMER || reservationEntity.getStatus() == ReservationStatus.EN_ATTENTE){
//TODO : THE CLIENT CAN'T CANCEL HIS RESERVATION AFTER 24H BEFORE THE FLIGHT
reservationEntity.setStatus(ReservationStatus.ANNULER);

for (VolEntity vol : reservationEntity.getVolEntities()){
if (vol == null){
return false;
}
if (vol.getDateDepart().before(Util.addHours(24))){
return false;
}else {
//TODO : THE CLIENT WILL PAY 8% OF THE TOTAL COST OF THE FLIGHT
double totalCost = reservationEntity.getPrixTotal();
double cost = totalCost * 0.08;
reservationEntity.setPrixTotal(totalCost - cost);
return this.reservationDao.annulerReservation(reservationEntity);
}
}

}
}


return false;
}

@Override
public boolean envoyerEmail(ReservationEntity reservationEntity) {
if (reservationEntity == null){
return false;
}
String email = "[email protected]";
String subject = "Confirmation de votre réservation";
String message = "Bonjour Mr/Mme "+reservationEntity.getClient().getNom()+" "+reservationEntity.getClient().getPrenom()+"\n" +
"Nous vous confirmons votre réservation de vol avec la compagnie Air Afraik\n" +
"Votre code de réservation est : "+reservationEntity.getCode()+"\n" +
"Merci de votre confiance\n" +
"Cordialement\n" +
"Air Afraik";


return EmailProvider.sendMail(message,subject,email);

}

@Override
public int statisticsReservation(ReservationStatus reservationStatus,HashMap<String, String> conditions) {


//TODO : THE CLIENT WIL CHOOSE TWO DATE CALLED DATE_1 AND DATE_2 FROM THE CALENDER


if (reservationStatus == null || conditions == null){
return 0;
}

ArrayList<ReservationEntity> reservationEntities =
reservationDao.AfficheLesStatistiques(conditions, reservationStatus);


return reservationEntities.size();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
import ma.yc.airafraik.dao.Impl.VolDaoImpl;
import ma.yc.airafraik.dao.VolDao;
import ma.yc.airafraik.entities.VolEntity;
import ma.yc.airafraik.service.SearchVolsService;
import ma.yc.airafraik.service.VolsService;
import ma.yc.airafraik.service.VolService;

public class VolServiceImpl implements VolService {

private VolDao volDao;
private SearchVolsService searchVolsService;
private VolsService volsService;


public VolServiceImpl() {
this.volDao = new VolDaoImpl();
this.searchVolsService = new SearchVolsServiceImpl();
this.volsService = new VolsServiceImpl();
}

@Override
Expand All @@ -25,7 +25,7 @@ public boolean ajouterVol(VolEntity vol) {

@Override
public boolean supprimerVol(String id) {
VolEntity vol = this.searchVolsService.searchVolParId(id);
VolEntity vol = this.volsService.searchVolParId(id);
return this.volDao.supprimerVol(vol);
}
}
Loading

0 comments on commit d53681a

Please sign in to comment.