Skip to content

Commit 3ad8cb3

Browse files
committed
init
0 parents  commit 3ad8cb3

20 files changed

+1380
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.iml
2+
.idea/
3+
target/
4+
*.jar

pom.xml

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.apina.api</groupId>
6+
<artifactId>ApinaAPI</artifactId>
7+
<version>1.0.0</version>
8+
<packaging>jar</packaging>
9+
<name>ApinaAPI</name>
10+
11+
<description>Indoor climbing API</description>
12+
<parent>
13+
<groupId>org.springframework.boot</groupId>
14+
<artifactId>spring-boot-starter-parent</artifactId>
15+
<version>3.2.2</version>
16+
<relativePath/>
17+
</parent>
18+
19+
<profiles>
20+
<profile>
21+
<id>app</id>
22+
<properties>
23+
<start-class>com.apina.api.ApplicationStarter</start-class>
24+
</properties>
25+
</profile>
26+
<profile>
27+
<id>populator</id>
28+
<properties>
29+
<start-class>com.apina.api.util.DatabasePopulator</start-class>
30+
</properties>
31+
</profile>
32+
</profiles>
33+
34+
<properties>
35+
<java.version>17</java.version>
36+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
37+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
38+
<mongodb.version>4.11.1</mongodb.version>
39+
<maven-compiler-plugin.version>3.12.1</maven-compiler-plugin.version>
40+
<springdoc-openapi-starter-webmvc-ui.version>2.3.0</springdoc-openapi-starter-webmvc-ui.version>
41+
</properties>
42+
43+
<dependencies>
44+
<dependency>
45+
<groupId>org.mongodb</groupId>
46+
<artifactId>mongodb-driver-sync</artifactId>
47+
<version>${mongodb.version}</version>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.springframework.boot</groupId>
51+
<artifactId>spring-boot-starter-web</artifactId>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.springframework.boot</groupId>
55+
<artifactId>spring-boot-starter-test</artifactId>
56+
<scope>test</scope>
57+
</dependency>
58+
<dependency>
59+
<groupId>org.springdoc</groupId>
60+
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
61+
<version>${springdoc-openapi-starter-webmvc-ui.version}</version>
62+
</dependency>
63+
</dependencies>
64+
65+
<build>
66+
<plugins>
67+
<plugin>
68+
<groupId>org.springframework.boot</groupId>
69+
<artifactId>spring-boot-maven-plugin</artifactId>
70+
</plugin>
71+
<plugin>
72+
<groupId>org.apache.maven.plugins</groupId>
73+
<artifactId>maven-surefire-plugin</artifactId>
74+
<configuration>
75+
<skipTests>true</skipTests>
76+
</configuration>
77+
<executions>
78+
<execution>
79+
<id>unit-tests</id>
80+
<phase>test</phase>
81+
<goals>
82+
<goal>test</goal>
83+
</goals>
84+
<configuration>
85+
<skipTests>false</skipTests>
86+
<includes>
87+
<include>**/*Test.java</include>
88+
</includes>
89+
</configuration>
90+
</execution>
91+
<execution>
92+
<id>integration-tests</id>
93+
<phase>integration-test</phase>
94+
<goals>
95+
<goal>test</goal>
96+
</goals>
97+
<configuration>
98+
<skipTests>false</skipTests>
99+
<includes>
100+
<include>**/*IT.java</include>
101+
</includes>
102+
</configuration>
103+
</execution>
104+
</executions>
105+
</plugin>
106+
<plugin>
107+
<groupId>com.microsoft.azure</groupId>
108+
<artifactId>azure-webapp-maven-plugin</artifactId>
109+
<version>2.9.0</version>
110+
<configuration>
111+
<schemaVersion>v2</schemaVersion>
112+
<resourceGroup>cloud-shell-storage-westeurope</resourceGroup>
113+
<appName>ApinaAPI</appName>
114+
<pricingTier>B1</pricingTier>
115+
<region>westeurope</region>
116+
<runtime>
117+
<os>Linux</os>
118+
<javaVersion>Java 17</javaVersion>
119+
<webContainer>Java SE</webContainer>
120+
</runtime>
121+
<deployment>
122+
<resources>
123+
<resource>
124+
<directory>${project.basedir}/target</directory>
125+
<includes>
126+
<include>*.jar</include>
127+
</includes>
128+
</resource>
129+
</resources>
130+
</deployment>
131+
</configuration>
132+
</plugin>
133+
</plugins>
134+
</build>
135+
136+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.apina.api;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class ApplicationStarter {
8+
public static void main(String[] args) {
9+
SpringApplication.run(ApplicationStarter.class, args);
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.apina.api;
2+
3+
import com.mongodb.ConnectionString;
4+
import com.mongodb.MongoClientSettings;
5+
import com.mongodb.client.MongoClient;
6+
import com.mongodb.client.MongoClients;
7+
import org.bson.codecs.configuration.CodecRegistry;
8+
import org.bson.codecs.pojo.PojoCodecProvider;
9+
import org.springframework.beans.factory.annotation.Value;
10+
import org.springframework.context.annotation.Bean;
11+
import org.springframework.context.annotation.Configuration;
12+
13+
import static org.bson.codecs.configuration.CodecRegistries.fromProviders;
14+
import static org.bson.codecs.configuration.CodecRegistries.fromRegistries;
15+
16+
@Configuration
17+
public class MongoDBConfiguration {
18+
19+
@Value("${spring.data.mongodb.uri}")
20+
private String connectionString;
21+
22+
@Bean
23+
public MongoClient mongoClient() {
24+
CodecRegistry pojoCodecRegistry = fromProviders(PojoCodecProvider.builder().automatic(true).build());
25+
CodecRegistry codecRegistry = fromRegistries(MongoClientSettings.getDefaultCodecRegistry(), pojoCodecRegistry);
26+
return MongoClients.create(MongoClientSettings.builder()
27+
.applyConnectionString(new ConnectionString(connectionString))
28+
.codecRegistry(codecRegistry)
29+
.build());
30+
}
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.apina.api.config;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.web.servlet.config.annotation.CorsRegistry;
5+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
6+
7+
@Configuration
8+
public class WebConfig implements WebMvcConfigurer {
9+
10+
@Override
11+
public void addCorsMappings(CorsRegistry registry) {
12+
registry.addMapping("/api/**")
13+
.allowedOrigins("https://rasmushy.github.io/apinaapi")
14+
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
15+
.allowedHeaders("*")
16+
.allowCredentials(true);
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.apina.api.controllers;
2+
3+
import com.apina.api.dtos.GymDTO;
4+
import com.apina.api.services.GymService;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.springframework.http.HttpStatus;
8+
import org.springframework.http.ResponseEntity;
9+
import org.springframework.web.bind.annotation.*;
10+
11+
import java.util.List;
12+
@RestController
13+
@RequestMapping("/api")
14+
public class GymController {
15+
private static final Logger LOGGER = LoggerFactory.getLogger(GymController.class);
16+
private final GymService gymService;
17+
18+
public GymController(GymService gymService) {
19+
this.gymService = gymService;
20+
}
21+
22+
@PostMapping("gym")
23+
@ResponseStatus(HttpStatus.CREATED)
24+
public GymDTO postGym(@RequestBody GymDTO gymDTO) {
25+
return gymService.save(gymDTO);
26+
}
27+
28+
@PostMapping("gyms")
29+
@ResponseStatus(HttpStatus.CREATED)
30+
public List<GymDTO> postGyms(@RequestBody List<GymDTO> gymEntities) {
31+
return gymService.saveAll(gymEntities);
32+
}
33+
34+
@GetMapping("gyms")
35+
public List<GymDTO> getGyms() {
36+
return gymService.findAll();
37+
}
38+
39+
@GetMapping("gym/{id}")
40+
public ResponseEntity<GymDTO> getGym(@PathVariable String id) {
41+
GymDTO gymDTO = gymService.findOne(id);
42+
if (gymDTO == null)
43+
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
44+
return ResponseEntity.ok(gymDTO);
45+
}
46+
47+
@GetMapping("gyms/{ids}")
48+
public List<GymDTO> getGyms(@PathVariable String ids) {
49+
List<String> listIds = List.of(ids.split(","));
50+
return gymService.findAll(listIds);
51+
}
52+
53+
@GetMapping("gyms/city/{city}")
54+
public List<GymDTO> getGymsByCity(@PathVariable String city) {
55+
return gymService.findAllByCity(city);
56+
}
57+
58+
@GetMapping("gyms/company/{company}")
59+
public List<GymDTO> getGymsByCompany(@PathVariable String company) {
60+
return gymService.findAllByCompany(company);
61+
}
62+
63+
@GetMapping("gyms/openingTime/{openingTime}")
64+
public List<GymDTO> getGymsByOpeningTime(@PathVariable String openingTime) {
65+
return gymService.findAllByOpeningTime(openingTime);
66+
}
67+
68+
@GetMapping("gyms/closingTime/{closingTime}")
69+
public List<GymDTO> getGymsByClosingTime(@PathVariable String closingTime) {
70+
return gymService.findAllByClosingTime(closingTime);
71+
}
72+
73+
@GetMapping("gyms/address/{streetName}")
74+
public GymDTO getGymByAddress(@PathVariable String streetName) {
75+
return gymService.findByAddress(streetName);
76+
}
77+
78+
@PutMapping("gym")
79+
public GymDTO putGym(@RequestBody GymDTO gymDTO) {
80+
return gymService.update(gymDTO);
81+
}
82+
83+
@PutMapping("gyms")
84+
public Long putGyms(@RequestBody List<GymDTO> gymEntities) {
85+
return gymService.update(gymEntities);
86+
}
87+
88+
@DeleteMapping("gyms")
89+
public Long deleteGyms() {
90+
return gymService.deleteAll();
91+
}
92+
93+
@DeleteMapping("gym/{id}")
94+
public Long deleteGym(@PathVariable String id) {
95+
return gymService.delete(id);
96+
}
97+
98+
@DeleteMapping("gyms/{ids}")
99+
public Long deleteGyms(@PathVariable String ids) {
100+
List<String> listIds = List.of(ids.split(","));
101+
return gymService.delete(listIds);
102+
}
103+
104+
@GetMapping("gyms/count")
105+
public Long getCount() {
106+
return gymService.count();
107+
}
108+
109+
@ExceptionHandler(RuntimeException.class)
110+
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
111+
public final Exception handleAllExceptions(RuntimeException e) {
112+
LOGGER.error("Internal server error.", e);
113+
return e;
114+
}
115+
116+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.apina.api.dtos;
2+
3+
import com.apina.api.models.AddressEntity;
4+
5+
public record AddressDTO(int number, String street, String postcode, String city) {
6+
7+
public AddressDTO(AddressEntity a) {
8+
this(a.getNumber(), a.getStreet(), a.getPostcode(), a.getCity());
9+
}
10+
11+
public AddressEntity toAddressEntity() {
12+
return new AddressEntity(number, street, postcode, city);
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.apina.api.dtos;
2+
3+
import com.apina.api.models.CompanyEntity;
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
public record CompanyDTO(
8+
String homePage,
9+
String name,
10+
Map<String, String> prices) {
11+
12+
public CompanyDTO(CompanyEntity c) {
13+
this(c.getHomePage(), c.getName(), c.getPrices());
14+
}
15+
16+
public CompanyEntity toCompanyEntity() {
17+
return new CompanyEntity(homePage, name, prices);
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.apina.api.dtos;
2+
3+
import com.apina.api.models.GymEntity;
4+
import org.bson.types.ObjectId;
5+
6+
import java.util.Date;
7+
public record GymDTO(
8+
String id,
9+
CompanyDTO company,
10+
AddressDTO address,
11+
Date createdAt,
12+
String openingTime,
13+
String closingTime
14+
) {
15+
16+
public GymDTO(GymEntity g) {
17+
this(g.getId().toString(), new CompanyDTO(g.getCompany()), new AddressDTO(g.getAddress()), g.getCreatedAt(), g.getOpeningTime(), g.getClosingTime());
18+
}
19+
20+
public GymEntity toGymEntity() {
21+
ObjectId _id = id == null ? new ObjectId() : new ObjectId(id);
22+
return new GymEntity(_id, company.toCompanyEntity(), address.toAddressEntity(), createdAt, openingTime, closingTime);
23+
}
24+
}

0 commit comments

Comments
 (0)