diff --git a/src/main/java/com/MeetMate/appointment/Appointment.java b/src/main/java/com/MeetMate/appointment/Appointment.java index bb730c8..f940601 100644 --- a/src/main/java/com/MeetMate/appointment/Appointment.java +++ b/src/main/java/com/MeetMate/appointment/Appointment.java @@ -21,4 +21,10 @@ public class Appointment { String description; String location; AppointmentStatus status; + + public Appointment(long id, long companyID, long clientID) { + this.id = id; + this.companyID = companyID; + this.clientID = clientID; + } } diff --git a/src/main/java/com/MeetMate/appointment/AppointmentConfig.java b/src/main/java/com/MeetMate/appointment/AppointmentConfig.java new file mode 100644 index 0000000..49cff69 --- /dev/null +++ b/src/main/java/com/MeetMate/appointment/AppointmentConfig.java @@ -0,0 +1,24 @@ +//package com.MeetMate.appointment; +// +//import com.MeetMate.company.CompanyRepository; +//import org.springframework.boot.CommandLineRunner; +//import org.springframework.context.annotation.Bean; +//import org.springframework.context.annotation.Configuration; +// +//@Configuration +//public class AppointmentConfig { +// +// @Bean +// public CommandLineRunner run(AppointmentRepository appointmentRepository) throws Exception { +// System.out.println("Yeehaaaa"); +// return args -> { +// Appointment appointment = new Appointment(); +// appointment.setId(1L); +// appointment.setCompanyID(1L); +// +// appointmentRepository.save(appointment); +// }; +// } +// +// +//} diff --git a/src/main/java/com/MeetMate/appointment/AppointmentController.java b/src/main/java/com/MeetMate/appointment/AppointmentController.java index 07522be..17036eb 100644 --- a/src/main/java/com/MeetMate/appointment/AppointmentController.java +++ b/src/main/java/com/MeetMate/appointment/AppointmentController.java @@ -1,30 +1,75 @@ -//package com.MeetMate.appointment; +package com.MeetMate.appointment; + +import lombok.RequiredArgsConstructor; +import org.springframework.graphql.data.method.annotation.Argument; +import org.springframework.graphql.data.method.annotation.MutationMapping; +import org.springframework.graphql.data.method.annotation.QueryMapping; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; +import org.springframework.web.bind.annotation.RequestMapping; + +import java.lang.reflect.InaccessibleObjectException; +import java.util.Map; + +@Controller +@RequestMapping(path = "api/appointment") +@RequiredArgsConstructor +public class AppointmentController { + private final AppointmentService appointmentService; + + @QueryMapping + public Appointment getAppointment(@Argument long id) { + try { + return appointmentService.getAppointment(id); + + } catch (Throwable t) { + Class tc = t.getClass(); + return null; +// if (tc == EntityNotFoundException.class) +// return ResponseEntity.status(HttpStatus.NOT_FOUND).body("message: " + t.getMessage()); // -//import com.MeetMate.company.Company; -//import lombok.RequiredArgsConstructor; -//import org.springframework.graphql.data.method.annotation.Argument; -//import org.springframework.graphql.data.method.annotation.QueryMapping; +// if (tc == IllegalArgumentException.class) +// return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("message: " + t.getMessage()); // -//@RequiredArgsConstructor -//public class AppointmentController { -// private final AppointmentService appointmentService; -// -// @QueryMapping -// public Company getAppointment(@Argument long id) { -// try { -// return appointmentService.getAppointment(id); -// -// } catch (Throwable t) { -// Class tc = t.getClass(); -// return null; -//// if (tc == EntityNotFoundException.class) -//// return ResponseEntity.status(HttpStatus.NOT_FOUND).body("message: " + t.getMessage()); -//// -//// if (tc == IllegalArgumentException.class) -//// return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("message: " + t.getMessage()); -//// -//// return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("message: " + t.getMessage()); -// } -// } -// -//} +// return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("message: " + t.getMessage()); + } + } + + @MutationMapping + public ResponseEntity createAppointment( + @Argument String from, + @Argument String to, + @Argument long companyId, + @Argument long clientId, + @Argument long assigneeId, +// @Argument Select Prompt → f.E. medical industry: Untersuchung, Operation, + @Argument String description, + @Argument String location, + @Argument String status) { + try { + Map appointmentData = Map.of( + "from", from, + "to", to, + "assigneeId", assigneeId, + // "prompt", Select Prompt → f.E. medical industry: Untersuchung, Operation); + "description", description, + "location", location, + "status", status + ); + + appointmentService.createAppointment(companyId, clientId, appointmentData); + return ResponseEntity.ok().build(); + + } catch (Throwable t) { + Class tc = t.getClass(); + if (tc == InaccessibleObjectException.class) + return ResponseEntity.status(HttpStatus.PRECONDITION_FAILED).body("message: " + t.getMessage()); + + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("message: " + t.getMessage()); + } + } + +} diff --git a/src/main/java/com/MeetMate/appointment/AppointmentRepository.java b/src/main/java/com/MeetMate/appointment/AppointmentRepository.java index 0c541a8..df8b72d 100644 --- a/src/main/java/com/MeetMate/appointment/AppointmentRepository.java +++ b/src/main/java/com/MeetMate/appointment/AppointmentRepository.java @@ -1,12 +1,12 @@ -//package com.MeetMate.appointment; -// -//import org.springframework.data.mongodb.repository.MongoRepository; -//import org.springframework.stereotype.Repository; -// -//import java.util.Optional; -// -//@Repository -//public interface AppointmentRepository extends MongoRepository { -// -// -//} +package com.MeetMate.appointment; + +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.stereotype.Repository; + +import java.util.Optional; + +@Repository +public interface AppointmentRepository extends MongoRepository { + + Optional findAppointmentById(long id); +} diff --git a/src/main/java/com/MeetMate/appointment/AppointmentService.java b/src/main/java/com/MeetMate/appointment/AppointmentService.java index 9cd9e05..853ba2f 100644 --- a/src/main/java/com/MeetMate/appointment/AppointmentService.java +++ b/src/main/java/com/MeetMate/appointment/AppointmentService.java @@ -1,4 +1,38 @@ -//package com.MeetMate.appointment; -// -//public class AppointmentService { -//} +package com.MeetMate.appointment; + +import com.MeetMate.appointment.sequence.AppointmentSequenceService; +import jakarta.persistence.EntityNotFoundException; +import jakarta.transaction.Transactional; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.lang.reflect.Field; +import java.util.Map; + +@Service +@RequiredArgsConstructor +public class AppointmentService { + + private final AppointmentRepository appointmentRepository; + private final AppointmentSequenceService appointmentSequenceService; + + public Appointment getAppointment(long id) { + return appointmentRepository.findAppointmentById(id) + .orElseThrow(() -> new EntityNotFoundException("Appointment not found")); + } + + @Transactional + public void createAppointment(long companyId, long clientId, Map appointmentData) throws IllegalAccessException { + long appointmentId = appointmentSequenceService.getCurrentValue(); + + Appointment appointment = new Appointment(appointmentId, companyId, clientId); + Field[] fields = Appointment.class.getDeclaredFields(); + for(Field field : fields) { + field.setAccessible(true); + field.set(appointment, appointmentData.get(field.getName())); + } + + appointmentRepository.save(appointment); + appointmentSequenceService.incrementId(); + } +} diff --git a/src/main/java/com/MeetMate/appointment/sequence/AppointmentSequence.java b/src/main/java/com/MeetMate/appointment/sequence/AppointmentSequence.java new file mode 100644 index 0000000..44c3e5e --- /dev/null +++ b/src/main/java/com/MeetMate/appointment/sequence/AppointmentSequence.java @@ -0,0 +1,16 @@ +package com.MeetMate.appointment.sequence; + +import lombok.AllArgsConstructor; +import lombok.Data; +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; + +@Document(collection = "appointment_sequence") +@Data +@AllArgsConstructor +public class AppointmentSequence { + @Id + String id; + long value; + +} \ No newline at end of file diff --git a/src/main/java/com/MeetMate/appointment/sequence/AppointmentSequenceService.java b/src/main/java/com/MeetMate/appointment/sequence/AppointmentSequenceService.java new file mode 100644 index 0000000..6d53231 --- /dev/null +++ b/src/main/java/com/MeetMate/appointment/sequence/AppointmentSequenceService.java @@ -0,0 +1,31 @@ +package com.MeetMate.appointment.sequence; + +import jakarta.transaction.Transactional; +import lombok.RequiredArgsConstructor; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.query.Criteria; +import org.springframework.data.mongodb.core.query.Query; +import org.springframework.data.mongodb.core.query.Update; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class AppointmentSequenceService { + + private final MongoTemplate mongoTemplate; + + @Transactional + public void incrementId() { + Query query = new Query(Criteria.where("_id").is("appointment_sequence")); + Update update = new Update().inc("value", 1); + mongoTemplate.updateFirst(query, update, AppointmentSequence.class); + } + + public long getCurrentValue(){ + Query query = new Query(Criteria.where("_id").is("appointment_sequence")); + AppointmentSequence sequence = mongoTemplate.findOne(query, AppointmentSequence.class); + return sequence.getValue(); + } + + +} diff --git a/src/main/java/com/MeetMate/company/CompanyService.java b/src/main/java/com/MeetMate/company/CompanyService.java index b4443f9..df1d702 100644 --- a/src/main/java/com/MeetMate/company/CompanyService.java +++ b/src/main/java/com/MeetMate/company/CompanyService.java @@ -1,6 +1,6 @@ package com.MeetMate.company; -import com.MeetMate.company.sequence.SequenceService; +import com.MeetMate.company.sequence.CompanySequenceService; import com.MeetMate.enums.BusinessType; import com.MeetMate.enums.UserRole; import com.MeetMate.security.JwtService; @@ -25,7 +25,7 @@ public class CompanyService { private final UserRepository userRepository; private final CompanyRepository companyRepository; private final MongoTemplate mongoTemplate; - private final SequenceService sequenceService; + private final CompanySequenceService companySequenceService; private final JwtService jwtService; public Company getCompany(long id) throws IllegalArgumentException { @@ -35,7 +35,7 @@ public Company getCompany(long id) throws IllegalArgumentException { @Transactional public void createCompany(String companyName, String ownerEmail, String ownerName, String ownerPassword) { - long companyId = sequenceService.getCurrentValue(); + long companyId = companySequenceService.getCurrentValue(); //Create the company owner MultiValueMap ownerData = new LinkedMultiValueMap<>(); ownerData.add("email", ownerEmail); @@ -47,7 +47,7 @@ public void createCompany(String companyName, String ownerEmail, String ownerNam userController.registerNewUser(ownerData); companyRepository.save(new Company(companyId, companyName, ownerEmail)); - sequenceService.incrementId(); + companySequenceService.incrementId(); } @Transactional diff --git a/src/main/java/com/MeetMate/company/sequence/SequenceConfig.java b/src/main/java/com/MeetMate/company/sequence/CompanySequenceConfig.java similarity index 64% rename from src/main/java/com/MeetMate/company/sequence/SequenceConfig.java rename to src/main/java/com/MeetMate/company/sequence/CompanySequenceConfig.java index fcc5e34..c3db1d6 100644 --- a/src/main/java/com/MeetMate/company/sequence/SequenceConfig.java +++ b/src/main/java/com/MeetMate/company/sequence/CompanySequenceConfig.java @@ -1,12 +1,13 @@ package com.MeetMate.company.sequence; +import com.MeetMate.appointment.sequence.AppointmentSequence; import org.springframework.boot.CommandLineRunner; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.mongodb.core.MongoTemplate; @Configuration -public class SequenceConfig { +public class CompanySequenceConfig { @Bean public CommandLineRunner init(MongoTemplate mongoTemplate) { @@ -15,6 +16,10 @@ public CommandLineRunner init(MongoTemplate mongoTemplate) { mongoTemplate.createCollection(CompanySequence.class); mongoTemplate.insert(new CompanySequence("company_sequence", 0)); } + if (!mongoTemplate.collectionExists(AppointmentSequence.class)) { + mongoTemplate.createCollection(AppointmentSequence.class); + mongoTemplate.insert(new AppointmentSequence("appointment_sequence", 0)); + } }; } } diff --git a/src/main/java/com/MeetMate/company/sequence/SequenceService.java b/src/main/java/com/MeetMate/company/sequence/CompanySequenceService.java similarity index 96% rename from src/main/java/com/MeetMate/company/sequence/SequenceService.java rename to src/main/java/com/MeetMate/company/sequence/CompanySequenceService.java index 3fc7b96..3468cb5 100644 --- a/src/main/java/com/MeetMate/company/sequence/SequenceService.java +++ b/src/main/java/com/MeetMate/company/sequence/CompanySequenceService.java @@ -10,7 +10,7 @@ @Service @RequiredArgsConstructor -public class SequenceService { +public class CompanySequenceService { private final MongoTemplate mongoTemplate; diff --git a/src/main/resources/graphql/schema.graphqls b/src/main/resources/graphql/schema.graphqls index f3c29ef..c5c80f6 100644 --- a/src/main/resources/graphql/schema.graphqls +++ b/src/main/resources/graphql/schema.graphqls @@ -6,6 +6,7 @@ type Query { getCompany(id: ID!): Company #Appointment Queries + getAppointment(id: ID!): Appointment } type Mutation { @@ -27,6 +28,17 @@ type Mutation { deleteCompany: String #Appointment Mutations + createAppointment( + from: String, + to: String, + companyID: ID!, + clientID: ID!, + assigneeID: ID, + # Select Prompt → f.E. medical industry: Untersuchung, Operation + description: String, + location: String + appointmentStatus: String + ): String } type Company { @@ -40,22 +52,13 @@ type Company { type Appointment { id: ID - from: DateTime - to: DateTime - companyI: ID + from: String + to: String + companyID: ID clientID: ID assigneeID: ID # Select Prompt → f.E. medical industry: Untersuchung, Operation description: String location: String AppointmentStatus: String -} - -type DateTime { - year: Int - month: Int - day: Int - hour: Int - minute: Int - second: Int } \ No newline at end of file