Skip to content

Commit

Permalink
Merge pull request #7 from Onlineberatung/feat/VIC-826_consultant_CRUD
Browse files Browse the repository at this point in the history
Feat/vic 826 consultant crud
  • Loading branch information
adnanalicic authored Jul 15, 2022
2 parents 314fc29 + 02ff841 commit a9a3404
Show file tree
Hide file tree
Showing 8 changed files with 464 additions and 115 deletions.
4 changes: 3 additions & 1 deletion .run/appointmentService.run.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="appointmentService clean compile" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot">
<option name="ACTIVE_PROFILES" value="local" />
<option name="ALTERNATIVE_JRE_PATH" value="11" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="true" />
<option name="ENABLE_JMX_AGENT" value="false" />
<module name="onlineBeratung-appointmentService" />
<module name="appointmentservice" />
<option name="SPRING_BOOT_MAIN_CLASS" value="com.vi.appointmentservice.AppointmentServiceApplication" />
<extension name="net.ashald.envfile">
<option name="IS_ENABLED" value="true" />
Expand Down
35 changes: 34 additions & 1 deletion api/appointmentService.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -774,4 +774,37 @@ components:
properties:
user:
type: string

CalcomSchedule:
type: object
properties:
id:
type: integer
format: int32
userId:
type: integer
format: int32
name:
type: string
timeZone:
type: string
CalcomAvailability:
type: object
properties:
id:
type: integer
format: int32
days:
type: array
items:
type: integer
format: int32
example: [1,2,3,4,5]
startTime:
type: string
example: 1970-01-01T09:00:00.000Z
endTime:
type: string
example: 1970-01-01T17:00:00.000Z
scheduleId:
type: integer
format: int32

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@
import com.vi.appointmentservice.service.CalComEventTypeService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.keycloak.authorization.client.util.Http;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
* Controller for event-type API operations.
*/
Expand Down Expand Up @@ -47,9 +43,9 @@ public ResponseEntity<Void> deleteEventType(Long eventTypeId) {
public ResponseEntity<CalcomEventType> getEventTypeById(Long eventTypeId) {
try {
CalcomEventType result = calComEventTypeService.getEventTypeById(eventTypeId);
if(result != null){
if (result != null) {
return new ResponseEntity<>(calComEventTypeService.getEventTypeById(eventTypeId), HttpStatus.OK);
}else{
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
} catch (JsonProcessingException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.vi.appointmentservice.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vi.appointmentservice.api.model.CalcomAvailability;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
@Slf4j
public class CalComAvailabilityService extends CalComService {

public CalComAvailabilityService(@NonNull RestTemplate restTemplate,
@Value("${calcom.apiUrl}") String calcomApiUrl,
@Value("${calcom.apiKey}") String calcomApiKey, @NonNull ObjectMapper objectMapper) {
super(restTemplate, calcomApiUrl, calcomApiKey);
}

public List<CalcomAvailability> getAllAvailabilities() throws JsonProcessingException {
String response = this.restTemplate.getForObject(this.buildUri("/v1/availabilities"),
String.class);
JSONObject jsonObject = new JSONObject(response);
response = jsonObject.getJSONArray("availabilities").toString();
ObjectMapper mapper = new ObjectMapper();
List<CalcomAvailability> result = mapper.readValue(response,
new TypeReference<List<CalcomAvailability>>() {
});
return result;
}

public CalcomAvailability getAvailabilityById(Long availabilityId)
throws JsonProcessingException {
List<CalcomAvailability> result = this.getAllAvailabilities();
return new ArrayList<>(result).stream()
.filter(availability -> availability.getId() == availabilityId.intValue())
.collect(Collectors.toList()).get(0);
}

public CalcomAvailability createAvailability(JSONObject availability) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> request = new HttpEntity<>(availability.toString(), headers);
return restTemplate.postForEntity(this.buildUri("/v1/availabilities"), request,
CalcomAvailability.class).getBody();
}

public CalcomAvailability editAvailability(JSONObject availability) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> request = new HttpEntity<>(availability.toString(), headers);
return restTemplate.postForEntity(this.buildUri(
"/v1/availabilities/" + availability.get("userId") + "_" + availability.get("teamId")),
request, CalcomAvailability.class).getBody();
}

public void deleteAvailability(Integer availabilityId) {
restTemplate.exchange(this.buildUri("/v1/availabilities/" + availabilityId),
HttpMethod.DELETE, null, String.class).getStatusCode();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,25 @@ public List<CalcomEventType> getAllEventTypes() throws JsonProcessingException {
return result;
}

public void deleteAllEventTypesOfUser(Long userId) throws JsonProcessingException {
List<CalcomEventType> eventTypesOfUser = new ArrayList<>(this.getAllEventTypes()).stream()
.filter(eventType -> eventType.getUserId() != null && eventType.getUserId() == userId.intValue())
.collect(Collectors.toList());
for (CalcomEventType eventType : eventTypesOfUser) {
this.deleteEventType(Long.valueOf(eventType.getId()));
}
}

public List<CalcomEventType> getAllEventTypesOfTeam(Long teamId) throws JsonProcessingException {
String response = this.restTemplate.getForObject(this.buildUri("/v1/event-types"), String.class);
JSONObject jsonObject = new JSONObject(response);
response = jsonObject.getJSONArray("event_types").toString();
ObjectMapper mapper = new ObjectMapper();
List<CalcomEventType> result = mapper.readValue(response, new TypeReference<List<CalcomEventType>>(){});
List<CalcomEventType> result = this.getAllEventTypes();
// TODO: Add correct filter: .filter(eventType -> eventType.getTeamId() != null && eventType.getTeamId() == teamId.intValue())
return new ArrayList<>(result).stream()
.filter(eventType -> eventType.getTeamId() != null)
.collect(Collectors.toList());
}

public List<CalcomEventType> getAllEventTypesOfUser(Long userId) throws JsonProcessingException {
String response = this.restTemplate.getForObject(this.buildUri("/v1/event-types"), String.class);
JSONObject jsonObject = new JSONObject(response);
response = jsonObject.getJSONArray("event_types").toString();
ObjectMapper mapper = new ObjectMapper();
List<CalcomEventType> result = mapper.readValue(response, new TypeReference<List<CalcomEventType>>(){});
List<CalcomEventType> result = this.getAllEventTypes();
return new ArrayList<>(result).stream()
.filter(eventType -> eventType.getUserId() != null && eventType.getUserId() == userId.intValue())
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.vi.appointmentservice.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vi.appointmentservice.api.model.CalcomMembership;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
@Slf4j
public class CalComMembershipService extends CalComService {

public CalComMembershipService(@NonNull RestTemplate restTemplate,
@Value("${calcom.apiUrl}") String calcomApiUrl,
@Value("${calcom.apiKey}") String calcomApiKey, @NonNull ObjectMapper objectMapper) {
super(restTemplate, calcomApiUrl, calcomApiKey);
}

public List<CalcomMembership> getAllMemberships() throws JsonProcessingException {
String response = this.restTemplate.getForObject(this.buildUri("/v1/memberships"),
String.class);
JSONObject jsonObject = new JSONObject(response);
response = jsonObject.getJSONArray("memberships").toString();
ObjectMapper mapper = new ObjectMapper();
List<CalcomMembership> result = mapper.readValue(response,
new TypeReference<List<CalcomMembership>>() {
});
return result;
}

public void deleteAllMembershipsOfUser(Long userId) throws JsonProcessingException {
List<CalcomMembership> membershipsOfUser = new ArrayList<>(this.getAllMemberships()).stream()
.filter(membership -> membership.getUserId() != null
&& membership.getUserId() == userId.intValue())
.collect(Collectors.toList());
for (CalcomMembership membership : membershipsOfUser) {
this.deleteMembership(membership.getUserId(), membership.getTeamId());
}
}

public List<CalcomMembership> getAllMembershipsOfUser(Long userId)
throws JsonProcessingException {
List<CalcomMembership> result = this.getAllMemberships();
return new ArrayList<>(result).stream()
.filter(membership -> membership.getUserId() != null
&& membership.getUserId() == userId.intValue())
.collect(Collectors.toList());
}


public CalcomMembership createMembership(JSONObject membership) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> request = new HttpEntity<>(membership.toString(), headers);
return restTemplate.postForEntity(this.buildUri("/v1/memberships"), request,
CalcomMembership.class).getBody();
}

public CalcomMembership editMembership(JSONObject membership) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> request = new HttpEntity<>(membership.toString(), headers);
return restTemplate.postForEntity(this.buildUri(
"/v1/memberships/" + membership.get("userId") + "_" + membership.get("teamId")), request,
CalcomMembership.class).getBody();
}

public void deleteMembership(Long userId, Long teamId) {
restTemplate.exchange(this.buildUri("/v1/memberships/" + userId + "_" + teamId),
HttpMethod.DELETE, null, String.class).getStatusCode();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.vi.appointmentservice.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vi.appointmentservice.api.model.CalcomSchedule;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
@Slf4j
public class CalComScheduleService extends CalComService {

public CalComScheduleService(@NonNull RestTemplate restTemplate,
@Value("${calcom.apiUrl}") String calcomApiUrl,
@Value("${calcom.apiKey}") String calcomApiKey, @NonNull ObjectMapper objectMapper) {
super(restTemplate, calcomApiUrl, calcomApiKey);
}

public List<CalcomSchedule> getAllSchedules() throws JsonProcessingException {
String response = this.restTemplate.getForObject(this.buildUri("/v1/schedules"), String.class);
JSONObject jsonObject = new JSONObject(response);
response = jsonObject.getJSONArray("schedules").toString();
ObjectMapper mapper = new ObjectMapper();
List<CalcomSchedule> result = mapper.readValue(response,
new TypeReference<List<CalcomSchedule>>() {
});
return result;
}

public List<Integer> deleteAllSchedulesOfUser(Long userId) throws JsonProcessingException {
ArrayList<Integer> scheduleList = new ArrayList<>();
List<CalcomSchedule> schedulesOfUser = new ArrayList<>(this.getAllSchedules()).stream()
.filter(
schedule -> schedule.getUserId() != null && schedule.getUserId() == userId.intValue())
.collect(Collectors.toList());
for (CalcomSchedule schedule : schedulesOfUser) {
this.deleteSchedule(schedule.getId());
scheduleList.add(schedule.getId());
}
return scheduleList;
}

public List<CalcomSchedule> getAllSchedulesOfUser(Long userId) throws JsonProcessingException {
List<CalcomSchedule> result = this.getAllSchedules();
return new ArrayList<>(result).stream()
.filter(
schedule -> schedule.getUserId() != null && schedule.getUserId() == userId.intValue())
.collect(Collectors.toList());
}


public CalcomSchedule createSchedule(JSONObject schedule) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> request = new HttpEntity<>(schedule.toString(), headers);
return restTemplate.postForEntity(this.buildUri("/v1/schedules"), request, CalcomSchedule.class)
.getBody();
}

public CalcomSchedule editSchedule(JSONObject schedule) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> request = new HttpEntity<>(schedule.toString(), headers);
return restTemplate.postForEntity(this.buildUri("/v1/schedules/" + schedule.get("id")), request,
CalcomSchedule.class).getBody();
}

public void deleteSchedule(int scheduleId) {
restTemplate.exchange(this.buildUri("/v1/schedules/" + scheduleId),
HttpMethod.DELETE, null, String.class).getStatusCode();
}

}

0 comments on commit a9a3404

Please sign in to comment.