Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ipk 237 appointment queries #60

Merged
merged 4 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/main/java/com/MeetMate/appointment/Appointment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/MeetMate/appointment/AppointmentConfig.java
Original file line number Diff line number Diff line change
@@ -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);
// };
// }
//
//
//}
101 changes: 73 additions & 28 deletions src/main/java/com/MeetMate/appointment/AppointmentController.java
Original file line number Diff line number Diff line change
@@ -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<? extends Throwable> 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<? extends Throwable> 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<String, Object> 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<? extends Throwable> 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());
}
}

}
24 changes: 12 additions & 12 deletions src/main/java/com/MeetMate/appointment/AppointmentRepository.java
Original file line number Diff line number Diff line change
@@ -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<Appointment, Long> {
//
//
//}
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<Appointment, Long> {

Optional<Appointment> findAppointmentById(long id);
}
42 changes: 38 additions & 4 deletions src/main/java/com/MeetMate/appointment/AppointmentService.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,38 @@
//package com.MeetMate.appointment;
//
//public class AppointmentService {
//}
package com.MeetMate.appointment;

import com.MeetMate.appointment.sequence.SequenceService;
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 SequenceService sequenceService;

public Appointment getAppointment(long id) {
return appointmentRepository.findAppointmentById(id)
.orElseThrow(() -> new EntityNotFoundException("Appointment not found"));
}

@Transactional
public void createAppointment(long companyId, long clientId, Map<String, Object> appointmentData) throws IllegalAccessException {
long appointmentId = sequenceService.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);
sequenceService.incrementId();
}
}
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.MeetMate.appointment.sequence;

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 {

@Bean
public CommandLineRunner init(MongoTemplate mongoTemplate) {
return args -> {
if (!mongoTemplate.collectionExists(AppointmentSequence.class)) {
mongoTemplate.createCollection(AppointmentSequence.class);
mongoTemplate.insert(new AppointmentSequence("appointment_sequence", 0));
}
};
}
}
Original file line number Diff line number Diff line change
@@ -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 SequenceService {

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();
}


}
27 changes: 15 additions & 12 deletions src/main/resources/graphql/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type Query {
getCompany(id: ID!): Company

#Appointment Queries
getAppointment(id: ID!): Appointment
}

type Mutation {
Expand All @@ -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 {
Expand All @@ -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
}
Loading