Projekt

Obecné

Profil

« Předchozí | Další » 

Revize d8cbbf32

Přidáno uživatelem Lukas Cerny před téměř 6 roky(ů)

Re #7386 Implemented APIs endpoints with DTOs

Zobrazit rozdíly:

.gitignore
1
# Compiled class file
2
*.class
3

  
4
# Log file
5
*.log
6

  
7
# BlueJ files
8
*.ctxt
9

  
10
# Mobile Tools for Java (J2ME)
11
.mtj.tmp/
12

  
13
# Package Files #
14
*.jar
15
*.war
16
*.nar
17
*.ear
18
*.zip
19
*.tar.gz
20
*.rar
21

  
22
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23
hs_err_pid*
24

  
25
# IntelliJ
26
.idea/* 
27
*.iml
28
 
1
# Compiled class file
2
*.class
3

  
4
# Log file
5
*.log
6

  
7
# BlueJ files
8
*.ctxt
9

  
10
# Mobile Tools for Java (J2ME)
11
.mtj.tmp/
12

  
13
# Package Files #
14
*.jar
15
*.war
16
*.nar
17
*.ear
18
*.zip
19
*.tar.gz
20
*.rar
21

  
22
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23
hs_err_pid*
24

  
25
# IntelliJ
26
server/.idea/* 
27
server/*.iml
28

  
29
server/target/*
build.sh
1
mvn package && docker restart yamanager_app
1
mvn -f ./server/pom.xml clean package; docker restart yamanager_app
docker-compose.yml
7 7
    container_name: yamanager_app
8 8
    volumes:
9 9
      - /tmp
10
      - ./target/ymanager-1.0-SNAPSHOT.jar:/app.jar
10
      - ./server/target/ymanager-1.0-SNAPSHOT-full-app.jar:/app.jar
11 11
    entrypoint: java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005 -Djava.security.egd=file:/dev/./urandom -jar /app.jar
12 12
    restart: always
13 13
    ports:
......
23 23
    container_name: yamanager_db
24 24
    restart: always
25 25
    volumes:
26
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql
26
      - ./server/init.sql:/docker-entrypoint-initdb.d/init.sql
27 27
    environment:
28 28
      MYSQL_ROOT_PASSWORD: root
29 29
      MYSQL_USER: user
init.sql
1
CREATE TABLE test (
2
  id MEDIUMINT NOT NULL AUTO_INCREMENT,
3
  message VARCHAR(255),
4
  PRIMARY KEY (id)
5
);
6

  
7
INSERT INTO test (message) VALUES ('test 1');
8
INSERT INTO test (message) VALUES ('test 2');
9
INSERT INTO test (message) VALUES ('test 3');
10
INSERT INTO test (message) VALUES ('test 4');
install.sh
1
docker-compose up
1
mvn -f ./server/pom.xml clean package; docker-compose up
pom.xml
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"
3
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4
    <modelVersion>4.0.0</modelVersion>
5

  
6
    <groupId>cz.zcu.fav.kiv</groupId>
7
    <artifactId>ymanager</artifactId>
8
    <version>1.0-SNAPSHOT</version>
9

  
10
    <parent>
11
        <groupId>org.springframework.boot</groupId>
12
        <artifactId>spring-boot-starter-parent</artifactId>
13
        <version>2.0.5.RELEASE</version>
14
    </parent>
15

  
16
    <properties>
17
        <java.version>1.8</java.version>
18
    </properties>
19

  
20
    <dependencies>
21
        <dependency>
22
            <groupId>org.springframework.boot</groupId>
23
            <artifactId>spring-boot-starter-web</artifactId>
24
        </dependency>
25

  
26
        <!-- DATABASE -->
27
        <dependency>
28
            <groupId>org.springframework.boot</groupId>
29
            <artifactId>spring-boot-starter-jdbc</artifactId>
30
        </dependency>
31
        <dependency>
32
            <groupId>org.mariadb.jdbc</groupId>
33
            <artifactId>mariadb-java-client</artifactId>
34
            <version>2.4.1</version>
35
        </dependency>
36

  
37

  
38
        <!-- TESTS -->
39
        <dependency>
40
            <groupId>org.springframework.boot</groupId>
41
            <artifactId>spring-boot-starter-test</artifactId>
42
            <scope>test</scope>
43
        </dependency>
44
        <dependency>
45
            <groupId>org.junit.jupiter</groupId>
46
            <artifactId>junit-jupiter-api</artifactId>
47
            <scope>test</scope>
48
        </dependency>
49
        <dependency>
50
            <groupId>org.mockito</groupId>
51
            <artifactId>mockito-core</artifactId>
52
            <version>2.25.1</version>
53
            <scope>test</scope>
54
        </dependency>
55
    </dependencies>
56

  
57

  
58
    <build>
59
        <plugins>
60
            <plugin>
61
                <groupId>org.springframework.boot</groupId>
62
                <artifactId>spring-boot-maven-plugin</artifactId>
63
            </plugin>
64
        </plugins>
65
    </build>
66

  
67
</project>
server/init.sql
1
CREATE TABLE test (
2
  id MEDIUMINT NOT NULL AUTO_INCREMENT,
3
  message VARCHAR(255),
4
  PRIMARY KEY (id)
5
);
6

  
7
INSERT INTO test (message) VALUES ('test 1');
8
INSERT INTO test (message) VALUES ('test 2');
9
INSERT INTO test (message) VALUES ('test 3');
10
INSERT INTO test (message) VALUES ('test 4');
server/pom.xml
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"
3
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4
    <modelVersion>4.0.0</modelVersion>
5

  
6
    <groupId>cz.zcu.fav.kiv</groupId>
7
    <artifactId>ymanager</artifactId>
8
    <version>1.0-SNAPSHOT</version>
9

  
10
    <parent>
11
        <groupId>org.springframework.boot</groupId>
12
        <artifactId>spring-boot-starter-parent</artifactId>
13
        <version>2.0.5.RELEASE</version>
14
    </parent>
15

  
16
    <properties>
17
        <java.version>1.8</java.version>
18
    </properties>
19

  
20
    <dependencies>
21
        <dependency>
22
            <groupId>org.springframework.boot</groupId>
23
            <artifactId>spring-boot-starter-web</artifactId>
24
        </dependency>
25

  
26
        <!-- DATABASE -->
27
        <dependency>
28
            <groupId>org.springframework.boot</groupId>
29
            <artifactId>spring-boot-starter-jdbc</artifactId>
30
        </dependency>
31
        <dependency>
32
            <groupId>org.mariadb.jdbc</groupId>
33
            <artifactId>mariadb-java-client</artifactId>
34
            <version>2.4.1</version>
35
        </dependency>
36

  
37

  
38
        <!-- TESTS -->
39
        <dependency>
40
            <groupId>org.springframework.boot</groupId>
41
            <artifactId>spring-boot-starter-test</artifactId>
42
            <scope>test</scope>
43
        </dependency>
44
        <dependency>
45
            <groupId>org.junit.jupiter</groupId>
46
            <artifactId>junit-jupiter-api</artifactId>
47
            <scope>test</scope>
48
        </dependency>
49
        <dependency>
50
            <groupId>org.mockito</groupId>
51
            <artifactId>mockito-core</artifactId>
52
            <version>2.25.1</version>
53
            <scope>test</scope>
54
        </dependency>
55
    </dependencies>
56

  
57

  
58
    <build>
59
        <plugins>
60
            <plugin>
61
                <groupId>org.springframework.boot</groupId>
62
                <artifactId>spring-boot-maven-plugin</artifactId>
63
                <executions>
64
                    <execution>
65
                        <goals>
66
                            <goal>repackage</goal>
67
                        </goals>
68
                        <configuration>
69
                            <classifier>full-app</classifier>
70
                            <mainClass>cz.zcu.yamanager.Application</mainClass>
71
                        </configuration>
72
                    </execution>
73
                </executions>
74
            </plugin>
75
        </plugins>
76
    </build>
77

  
78
</project>
server/src/main/java/cz/zcu/yamanager/Application.java
1
package cz.zcu.yamanager;
2

  
3
import cz.zcu.yamanager.util.localization.Language;
4
import cz.zcu.yamanager.util.localization.Message;
5
import org.springframework.boot.SpringApplication;
6
import org.springframework.boot.autoconfigure.SpringBootApplication;
7

  
8
@SpringBootApplication
9
public class Application {
10

  
11
    public static void main(String[] args) {
12

  
13
        Message.config()
14
                .addLanguage(Language.EN)
15
                .addLanguage(Language.CZ);
16

  
17
        SpringApplication.run(Application.class, args);
18
    }
19
}
server/src/main/java/cz/zcu/yamanager/domain/Test.java
1
package cz.zcu.yamanager.domain;
2

  
3
public class Test {
4

  
5
    private final long id;
6
    private final String message;
7

  
8
    public Test(long id, String message) {
9
        this.id = id;
10
        this.message = message;
11
    }
12

  
13
    public long getId() {
14
        return id;
15
    }
16

  
17
    public String getMessage() {
18
        return message;
19
    }
20

  
21
    @Override
22
    public String toString() {
23
        return String.format("Test[id=%d, message='%s']", id, message);
24
    }
25
}
server/src/main/java/cz/zcu/yamanager/dto/AuthorizationRequest.java
1
package cz.zcu.yamanager.dto;
2

  
3
import java.time.LocalDateTime;
4

  
5
public class AuthorizationRequest {
6

  
7
    class User {
8
        public UserName name;
9
    }
10

  
11
    private long id;
12
    private User user;
13
    private LocalDateTime date;
14

  
15
    public long getId() {
16
        return id;
17
    }
18

  
19
    public void setId(long id) {
20
        this.id = id;
21
    }
22

  
23
    public User getUser() {
24
        return user;
25
    }
26

  
27
    public void setUserName(UserName name) {
28
        this.user = new User();
29
        this.user.name = name;
30
    }
31

  
32
    public LocalDateTime getDate() {
33
        return date;
34
    }
35

  
36
    public void setDate(LocalDateTime date) {
37
        this.date = date;
38
    }
39
}
server/src/main/java/cz/zcu/yamanager/dto/BasicProfileUser.java
1
package cz.zcu.yamanager.dto;
2

  
3
import java.util.List;
4

  
5
public class BasicProfileUser {
6
    private long id;
7
    private UserName name;
8
    private String photo;
9
    private List<CalendarItem> calendar;
10

  
11
    public long getId() {
12
        return id;
13
    }
14

  
15
    public void setId(long id) {
16
        this.id = id;
17
    }
18

  
19
    public UserName getName() {
20
        return name;
21
    }
22

  
23
    public void setName(UserName name) {
24
        this.name = name;
25
    }
26

  
27
    public String getPhoto() {
28
        return photo;
29
    }
30

  
31
    public void setPhoto(String photo) {
32
        this.photo = photo;
33
    }
34

  
35
    public List<CalendarItem> getCalendar() {
36
        return calendar;
37
    }
38

  
39
    public void setCalendar(List<CalendarItem> calendar) {
40
        this.calendar = calendar;
41
    }
42
}
server/src/main/java/cz/zcu/yamanager/dto/BasicRequest.java
1
package cz.zcu.yamanager.dto;
2

  
3
public class BasicRequest {
4

  
5
    private long id;
6
    private RequestType type;
7
    private RequestStatus status;
8

  
9
    public long getId() {
10
        return id;
11
    }
12

  
13
    public void setId(long id) {
14
        this.id = id;
15
    }
16

  
17
    public RequestType getType() {
18
        return type;
19
    }
20

  
21
    public void setType(RequestType type) {
22
        this.type = type;
23
    }
24

  
25
    public RequestStatus getStatus() {
26
        return status;
27
    }
28

  
29
    public void setStatus(RequestStatus status) {
30
        this.status = status;
31
    }
32
}
server/src/main/java/cz/zcu/yamanager/dto/CalendarItem.java
1
package cz.zcu.yamanager.dto;
2

  
3
import java.time.LocalDate;
4
import java.time.LocalTime;
5

  
6
public class CalendarItem {
7

  
8
    private LocalDate date;
9
    private LocalTime from;
10
    private LocalTime to;
11
    private VacationType type;
12

  
13
    public LocalDate getDate() {
14
        return date;
15
    }
16

  
17
    public void setDate(LocalDate date) {
18
        this.date = date;
19
    }
20

  
21
    public LocalTime getFrom() {
22
        return from;
23
    }
24

  
25
    public void setFrom(LocalTime from) {
26
        this.from = from;
27
    }
28

  
29
    public LocalTime getTo() {
30
        return to;
31
    }
32

  
33
    public void setTo(LocalTime to) {
34
        this.to = to;
35
    }
36

  
37
    public VacationType getType() {
38
        return type;
39
    }
40

  
41
    public void setType(VacationType type) {
42
        this.type = type;
43
    }
44
}
server/src/main/java/cz/zcu/yamanager/dto/CalendarView.java
1
package cz.zcu.yamanager.dto;
2

  
3
import java.time.LocalDate;
4
import java.time.LocalTime;
5

  
6
public class CalendarView {
7

  
8
    private LocalDate date;
9
    private LocalTime from;
10
    private LocalTime to;
11
    private VacationType type;
12
    private RequestStatus status;
13

  
14
    public LocalDate getDate() {
15
        return date;
16
    }
17

  
18
    public void setDate(LocalDate date) {
19
        this.date = date;
20
    }
21

  
22
    public LocalTime getFrom() {
23
        return from;
24
    }
25

  
26
    public void setFrom(LocalTime from) {
27
        this.from = from;
28
    }
29

  
30
    public LocalTime getTo() {
31
        return to;
32
    }
33

  
34
    public void setTo(LocalTime to) {
35
        this.to = to;
36
    }
37

  
38
    public VacationType getType() {
39
        return type;
40
    }
41

  
42
    public void setType(VacationType type) {
43
        this.type = type;
44
    }
45

  
46
    public RequestStatus getStatus() {
47
        return status;
48
    }
49

  
50
    public void setStatus(RequestStatus status) {
51
        this.status = status;
52
    }
53
}
server/src/main/java/cz/zcu/yamanager/dto/CalendarViewType.java
1
package cz.zcu.yamanager.dto;
2

  
3
public enum  CalendarViewType {
4
    MONTH,WEEK
5
}
server/src/main/java/cz/zcu/yamanager/dto/DefaultSettings.java
1
package cz.zcu.yamanager.dto;
2

  
3
import java.time.LocalDateTime;
4

  
5
public class DefaultSettings {
6
    private VacationInfo sickDay;
7
    private LocalDateTime notification;
8

  
9
    public VacationInfo getSickDay() {
10
        return sickDay;
11
    }
12

  
13
    public void setSickDay(VacationInfo sickDay) {
14
        this.sickDay = sickDay;
15
    }
16

  
17
    public LocalDateTime getNotification() {
18
        return notification;
19
    }
20

  
21
    public void setNotification(LocalDateTime notification) {
22
        this.notification = notification;
23
    }
24
}
server/src/main/java/cz/zcu/yamanager/dto/FullUserProfile.java
1
package cz.zcu.yamanager.dto;
2

  
3
import java.time.LocalDateTime;
4

  
5
public class FullUserProfile {
6

  
7
    class Settings {
8
        public LocalDateTime notification;
9
    }
10

  
11
    private long id;
12
    private UserName name;
13
    private String photo;
14
    private Settings settings;
15
    private VacationInfo vacation;
16
    private VacationInfo sickDay;
17
    private UserStatus status;
18
    private UserRole role;
19

  
20
    public long getId() {
21
        return id;
22
    }
23

  
24
    public void setId(long id) {
25
        this.id = id;
26
    }
27

  
28
    public UserName getName() {
29
        return name;
30
    }
31

  
32
    public void setName(UserName name) {
33
        this.name = name;
34
    }
35

  
36
    public String getPhoto() {
37
        return photo;
38
    }
39

  
40
    public void setPhoto(String photo) {
41
        this.photo = photo;
42
    }
43

  
44
    public Settings getSettings() {
45
        return settings;
46
    }
47

  
48
    public void setNotification(LocalDateTime notification) {
49
        this.settings = new Settings();
50
        this.settings.notification = notification;
51
    }
52

  
53
    public VacationInfo getVacation() {
54
        return vacation;
55
    }
56

  
57
    public void setVacation(VacationInfo vacation) {
58
        this.vacation = vacation;
59
    }
60

  
61
    public VacationInfo getSickDay() {
62
        return sickDay;
63
    }
64

  
65
    public void setSickDay(VacationInfo sickDay) {
66
        this.sickDay = sickDay;
67
    }
68

  
69
    public UserStatus getStatus() {
70
        return status;
71
    }
72

  
73
    public void setStatus(UserStatus status) {
74
        this.status = status;
75
    }
76

  
77
    public UserRole getRole() {
78
        return role;
79
    }
80

  
81
    public void setRole(UserRole role) {
82
        this.role = role;
83
    }
84
}
server/src/main/java/cz/zcu/yamanager/dto/RequestStatus.java
1
package cz.zcu.yamanager.dto;
2

  
3
public enum  RequestStatus {
4
    ACCEPTED, PENDING, REJECTED
5
}
server/src/main/java/cz/zcu/yamanager/dto/RequestType.java
1
package cz.zcu.yamanager.dto;
2

  
3
public enum  RequestType {
4
    VACATION, AUTHORIZATION
5
}
server/src/main/java/cz/zcu/yamanager/dto/UserName.java
1
package cz.zcu.yamanager.dto;
2

  
3
public class UserName {
4
    private String first;
5
    private String last;
6

  
7
    public String getFirst() {
8
        return first;
9
    }
10

  
11
    public void setFirst(String first) {
12
        this.first = first;
13
    }
14

  
15
    public String getLast() {
16
        return last;
17
    }
18

  
19
    public void setLast(String last) {
20
        this.last = last;
21
    }
22
}
server/src/main/java/cz/zcu/yamanager/dto/UserRequest.java
1
package cz.zcu.yamanager.dto;
2

  
3
import java.util.List;
4

  
5
public class UserRequest {
6

  
7
    private List<VacationRequest> vacation;
8
    private List<AuthorizationRequest> authorization;
9

  
10
    public List<VacationRequest> getVacation() {
11
        return vacation;
12
    }
13

  
14
    public void setVacation(List<VacationRequest> vacation) {
15
        this.vacation = vacation;
16
    }
17

  
18
    public List<AuthorizationRequest> getAuthorization() {
19
        return authorization;
20
    }
21

  
22
    public void setAuthorization(List<AuthorizationRequest> authorization) {
23
        this.authorization = authorization;
24
    }
25
}
server/src/main/java/cz/zcu/yamanager/dto/UserRole.java
1
package cz.zcu.yamanager.dto;
2

  
3
public enum  UserRole {
4
    EMPLOYEE, EMPLOYER
5
}
server/src/main/java/cz/zcu/yamanager/dto/UserSettings.java
1
package cz.zcu.yamanager.dto;
2

  
3
public class UserSettings {
4

  
5
    private UserRole role;
6
    private VacationInfo vacation;
7
    private VacationInfo sickDay;
8

  
9
    public UserRole getRole() {
10
        return role;
11
    }
12

  
13
    public void setRole(UserRole role) {
14
        this.role = role;
15
    }
16

  
17
    public VacationInfo getVacation() {
18
        return vacation;
19
    }
20

  
21
    public void setVacation(VacationInfo vacation) {
22
        this.vacation = vacation;
23
    }
24

  
25
    public VacationInfo getSickDay() {
26
        return sickDay;
27
    }
28

  
29
    public void setSickDay(VacationInfo sickDay) {
30
        this.sickDay = sickDay;
31
    }
32
}
server/src/main/java/cz/zcu/yamanager/dto/UserStatus.java
1
package cz.zcu.yamanager.dto;
2

  
3
public enum UserStatus {
4
    AUTHORIZED, PENDING, REJECTED
5
}
server/src/main/java/cz/zcu/yamanager/dto/VacationInfo.java
1
package cz.zcu.yamanager.dto;
2

  
3
public class VacationInfo {
4
    private float value;
5
    private VacationUnit unit;
6

  
7
    public float getValue() {
8
        return value;
9
    }
10

  
11
    public void setValue(float value) {
12
        this.value = value;
13
    }
14

  
15
    public VacationUnit getUnit() {
16
        return unit;
17
    }
18

  
19
    public void setUnit(VacationUnit unit) {
20
        this.unit = unit;
21
    }
22
}
server/src/main/java/cz/zcu/yamanager/dto/VacationRequest.java
1
package cz.zcu.yamanager.dto;
2

  
3
import java.time.LocalDate;
4
import java.time.LocalTime;
5

  
6
public class VacationRequest {
7

  
8
    class User {
9
        public UserName name;
10
    }
11

  
12
    private long id;
13
    private User user;
14
    private LocalDate date;
15
    private LocalTime from;
16
    private LocalTime to;
17
    private VacationType type;
18
    private RequestStatus status;
19

  
20
    public long getId() {
21
        return id;
22
    }
23

  
24
    public void setId(long id) {
25
        this.id = id;
26
    }
27

  
28
    public User getUser() {
29
        return user;
30
    }
31

  
32
    public void setUserName(UserName name) {
33
        this.user = new User();
34
        this.user.name = name;
35
    }
36

  
37
    public LocalDate getDate() {
38
        return date;
39
    }
40

  
41
    public void setDate(LocalDate date) {
42
        this.date = date;
43
    }
44

  
45
    public LocalTime getFrom() {
46
        return from;
47
    }
48

  
49
    public void setFrom(LocalTime from) {
50
        this.from = from;
51
    }
52

  
53
    public LocalTime getTo() {
54
        return to;
55
    }
56

  
57
    public void setTo(LocalTime to) {
58
        this.to = to;
59
    }
60

  
61
    public VacationType getType() {
62
        return type;
63
    }
64

  
65
    public void setType(VacationType type) {
66
        this.type = type;
67
    }
68

  
69
    public RequestStatus getStatus() {
70
        return status;
71
    }
72

  
73
    public void setStatus(RequestStatus status) {
74
        this.status = status;
75
    }
76
}
server/src/main/java/cz/zcu/yamanager/dto/VacationType.java
1
package cz.zcu.yamanager.dto;
2

  
3
public enum VacationType {
4
    VACATION, SICKDAY
5
}
server/src/main/java/cz/zcu/yamanager/dto/VacationUnit.java
1
package cz.zcu.yamanager.dto;
2

  
3
public enum  VacationUnit {
4
    DAY, HOUR
5
}
server/src/main/java/cz/zcu/yamanager/repository/TestRepository.java
1
package cz.zcu.yamanager.repository;
2

  
3
import cz.zcu.yamanager.domain.Test;
4
import org.slf4j.Logger;
5
import org.slf4j.LoggerFactory;
6
import org.springframework.beans.factory.annotation.Autowired;
7
import org.springframework.jdbc.core.JdbcTemplate;
8
import org.springframework.stereotype.Repository;
9

  
10
import java.util.List;
11

  
12
@Repository
13
public class TestRepository {
14

  
15
    private static final Logger log = LoggerFactory.getLogger(TestRepository.class);
16

  
17
    private final JdbcTemplate jdbc;
18

  
19
    @Autowired
20
    public TestRepository(JdbcTemplate jdbc) {
21
        this.jdbc = jdbc;
22
    }
23

  
24
    public List<Test> all() {
25
        return jdbc.query("SELECT id, message FROM test",
26
                (rs, rowNum) -> new Test(rs.getLong("id"), rs.getString("message")));
27
    }
28
}
server/src/main/java/cz/zcu/yamanager/util/localization/Language.java
1
package cz.zcu.yamanager.util.localization;
2

  
3
import java.util.Locale;
4

  
5
public enum  Language {
6

  
7
    EN  (Locale.ENGLISH),
8
    CZ  (new Locale("cs", "")),
9

  
10
    ;
11
    private Locale locale;
12
    Language(Locale locale) {
13
        this.locale = locale;
14
    }
15

  
16
    public Locale getLocale() {
17
        return locale;
18
    }
19

  
20
    public static Language get(String lang) {
21
        return valueOf(lang.toUpperCase());
22
    }
23
}
server/src/main/java/cz/zcu/yamanager/util/localization/Message.java
1
package cz.zcu.yamanager.util.localization;
2

  
3
import java.util.HashMap;
4
import java.util.Map;
5
import java.util.ResourceBundle;
6

  
7
import static java.util.ResourceBundle.getBundle;
8

  
9
public final class Message {
10

  
11
    private static Map<Language, ResourceBundle> BUNDLES;
12

  
13
    static {
14
        BUNDLES = new HashMap<>();
15

  
16
        config().addLanguage(Language.EN);
17
    }
18

  
19
    public interface Config {
20
        Config addLanguage(Language language);
21
    }
22

  
23
    public static Config config() {
24
        return new Config() {
25

  
26
            @Override
27
            public Config addLanguage(Language language) {
28
                if (!BUNDLES.containsKey(language)) {
29
                    BUNDLES.put(language, getBundle("Message", language.getLocale()));
30
                }
31
                return this;
32
            }
33
        };
34
    }
35

  
36
    public static String getString(Language language, String key) {
37
        if (!BUNDLES.containsKey(language)) return language.name()+" SOURCE MISSING";
38
        return BUNDLES.get(language).getString(key);
39
    }
40

  
41
    public static String getString(String key) {
42
        return getString(Language.EN, key);
43
    }
44
}
server/src/main/java/cz/zcu/yamanager/ws/rest/ApiController.java
1
package cz.zcu.yamanager.ws.rest;
2

  
3
import cz.zcu.yamanager.dto.*;
4
import org.springframework.http.ResponseEntity;
5
import org.springframework.web.bind.annotation.*;
6

  
7
import java.time.LocalDate;
8
import java.time.LocalDateTime;
9
import java.time.LocalTime;
10
import java.util.ArrayList;
11
import java.util.List;
12

  
13
import static org.springframework.http.HttpStatus.OK;
14
import static org.springframework.http.ResponseEntity.ok;
15
import static org.springframework.web.bind.annotation.RequestMethod.GET;
16
import static org.springframework.web.bind.annotation.RequestMethod.POST;
17

  
18
@RestController
19
public class ApiController {
20

  
21
    // *********************** POST ****************************
22

  
23
    @RequestMapping(value = "/users", method=GET)
24
    public List<BasicProfileUser> users(@RequestParam("status") String status) {
25
        UserStatus userStatus = UserStatus.valueOf(status);
26

  
27
        List<BasicProfileUser> users = new ArrayList<>();
28

  
29
        BasicProfileUser user = new BasicProfileUser();
30
        user.setId(1);
31
        UserName userName = new UserName();
32
        userName.setFirst("Tomas");
33
        userName.setLast("Novak");
34
        user.setName(userName);
35
        user.setPhoto("https://st2.depositphotos.com/9223672/12056/v/950/depositphotos_120568236-stock-illustration-male-face-avatar-logo-template.jpg");
36

  
37
        List<CalendarItem> calendarItems = new ArrayList<>();
38

  
39
        CalendarItem calendarItem = new CalendarItem();
40
        calendarItem.setType(VacationType.SICKDAY);
41
        calendarItem.setDate(LocalDate.now());
42
        calendarItem.setFrom(LocalTime.now());
43
        calendarItem.setTo(LocalTime.now());
44
        calendarItems.add(calendarItem);
45
        user.setCalendar(calendarItems);
46

  
47
        users.add(user);
48

  
49
        return users;
50
    }
51

  
52
    @RequestMapping(value = "/user/{id}/profile", method=GET)
53
    public FullUserProfile userProfile(@PathVariable("id") String id) {
54
        long userId = Long.valueOf(id);
55
        FullUserProfile user = new FullUserProfile();
56

  
57
        user.setId(userId);
58
        UserName userName = new UserName();
59
        userName.setFirst("Tomas");
60
        userName.setLast("Novak");
61
        user.setName(userName);
62
        user.setPhoto("https://st2.depositphotos.com/9223672/12056/v/950/depositphotos_120568236-stock-illustration-male-face-avatar-logo-template.jpg");
63
        user.setRole(UserRole.EMPLOYEE);
64
        user.setNotification(LocalDateTime.of(2010, 5, 4, 16, 10));
65
        user.setStatus(UserStatus.AUTHORIZED);
66
        VacationInfo sickday = new VacationInfo();
67
        sickday.setValue(3);
68
        sickday.setUnit(VacationUnit.DAY);
69
        user.setSickDay(sickday);
70
        VacationInfo vacation = new VacationInfo();
71
        vacation.setValue(15);
72
        vacation.setUnit(VacationUnit.HOUR);
73
        user.setVacation(vacation);
74
        return user;
75
    }
76

  
77
    @RequestMapping(value = "/users/requests", method=GET)
78
    public UserRequest userRequests(@RequestParam("type") String type) {
79
        RequestType requestType = RequestType.valueOf(type);
80

  
81
        UserName userName = new UserName();
82
        userName.setFirst("Tomas");
83
        userName.setLast("Novak");
84

  
85
        UserRequest request = new UserRequest();
86

  
87
        List<AuthorizationRequest> authorization = new ArrayList<>();
88
        AuthorizationRequest authRequest = new AuthorizationRequest();
89
        authRequest.setId(1);
90
        authRequest.setUserName(userName);
91
        authRequest.setDate(LocalDateTime.now());
92

  
93
        authorization.add(authRequest);
94
        request.setAuthorization(authorization);
95

  
96
        List<VacationRequest> vacation = new ArrayList<>();
97
        VacationRequest vacRequest = new VacationRequest();
98
        vacRequest.setId(1);
99
        vacRequest.setUserName(userName);
100
        vacRequest.setStatus(RequestStatus.PENDING);
101
        vacRequest.setType(VacationType.SICKDAY);
102
        vacRequest.setDate(LocalDate.now());
103
        vacRequest.setFrom(LocalTime.of(9, 30));
104
        vacRequest.setTo(LocalTime.of(18, 30));
105

  
106
        vacation.add(vacRequest);
107
        request.setVacation(vacation);
108

  
109
        return request;
110
    }
111

  
112

  
113
    @RequestMapping(value = "/user/calendar", method=GET)
114
    public List<CalendarView> personalCalendarView(@RequestParam("viewType") String viewType, @RequestParam("value") String value) {
115
        CalendarViewType calendarViewType = CalendarViewType.valueOf(viewType);
116
        int numberOfView = Integer.valueOf(value);
117

  
118
        List<CalendarView> calendarViews = new ArrayList<>();
119

  
120
        CalendarView view = new CalendarView();
121
        view.setStatus(RequestStatus.PENDING);
122
        view.setType(VacationType.SICKDAY);
123
        view.setDate(LocalDate.now());
124
        view.setFrom(LocalTime.of(9, 30));
125
        view.setTo(LocalTime.of(18, 30));
126

  
127
        calendarViews.add(view);
128

  
129
        return calendarViews;
130
    }
131

  
132
    @RequestMapping(value = "/user/{id}/calendar", method=GET)
133
    public List<CalendarView> userCalendarView(@PathVariable("id") String id, @RequestParam("viewType") String viewType, @RequestParam("value") String value) {
134
        CalendarViewType calendarViewType = CalendarViewType.valueOf(viewType);
135
        int numberOfView = Integer.valueOf(value);
136
        long userId = Long.valueOf(id);
137

  
138
        List<CalendarView> calendarViews = new ArrayList<>();
139

  
140
        CalendarView view = new CalendarView();
141
        view.setStatus(RequestStatus.PENDING);
142
        view.setType(VacationType.SICKDAY);
143
        view.setDate(LocalDate.now());
144
        view.setFrom(LocalTime.of(9, 30));
145
        view.setTo(LocalTime.of(18, 30));
146

  
147
        calendarViews.add(view);
148

  
149
        return calendarViews;
150
    }
151

  
152
    @RequestMapping(value = "/settings/default", method=GET)
153
    public DefaultSettings defaultSettings() {
154

  
155
        DefaultSettings settings = new DefaultSettings();
156

  
157
        VacationInfo sickDay = new VacationInfo();
158
        sickDay.setUnit(VacationUnit.DAY);
159
        sickDay.setValue(3);
160
        settings.setSickDay(sickDay);
161
        settings.setNotification(LocalDateTime.now());
162

  
163
        return settings;
164
    }
165

  
166
    // *********************** POST ****************************
167

  
168
    @RequestMapping(value = "/user/calendar", method=POST)
169
    public ResponseEntity personalCalendarView(@RequestBody CalendarView calendarView) {
170

  
171
        if (calendarView == null) {
172
            return ResponseEntity.badRequest().build();
173
        }
174

  
175
        return ok(OK);
176
    }
177

  
178
    @RequestMapping(value = "/user/requests", method=POST)
179
    public ResponseEntity userRequest(@RequestBody BasicRequest request) {
180

  
181
        if (request == null) {
182
            return ResponseEntity.badRequest().build();
183
        }
184

  
185
        return ok(OK);
186
    }
187

  
188
    @RequestMapping(value = "/user/{id}/settings", method=POST)
189
    public ResponseEntity userSettings(@PathVariable("id") String id, @RequestBody UserSettings settings) {
190

  
191
        if (settings == null) {
192
            return ResponseEntity.badRequest().build();
193
        }
194

  
195
         return ok(OK);
196
    }
197

  
198

  
199
    @RequestMapping(value = "/settings/default", method=POST)
200
    public ResponseEntity defaultSettings(@RequestBody DefaultSettings settings) {
201

  
202
        if (settings == null) {
203
            return ResponseEntity.badRequest().build();
204
        }
205

  
206
        return ok(OK);
207
    }
208
}
server/src/main/java/cz/zcu/yamanager/ws/rest/TestController.java
1
package cz.zcu.yamanager.ws.rest;
2

  
3
import cz.zcu.yamanager.domain.Test;
4
import cz.zcu.yamanager.repository.TestRepository;
5
import org.springframework.beans.factory.annotation.Autowired;
6
import org.springframework.web.bind.annotation.RequestMapping;
7
import org.springframework.web.bind.annotation.RequestParam;
8
import org.springframework.web.bind.annotation.RestController;
9

  
10
import java.util.List;
11

  
12
import static cz.zcu.yamanager.util.localization.Language.get;
13
import static cz.zcu.yamanager.util.localization.Message.getString;
14
import static org.springframework.web.bind.annotation.RequestMethod.GET;
15

  
16
@RestController
17
public class TestController {
18

  
19
    private final TestRepository repository;
20

  
21
    @Autowired
22
    public TestController(TestRepository repository) {
23
        this.repository = repository;
24
    }
25

  
26
    @RequestMapping(value = "/hello", method=GET)
27
    public String hello(@RequestParam(value = "lang", required = false, defaultValue = "en") String lang) {
28
        return getString(get(lang), "hello");
29
    }
30

  
31
    @RequestMapping(value = "/database", method=GET)
32
    public List<Test> database() {
33
        return repository.all();
34
    }
35
}
server/src/main/java/cz/zcu/yamanager/ws/rest/WebConfiguration.java
1
package cz.zcu.yamanager.ws.rest;
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 WebConfiguration implements WebMvcConfigurer {
9

  
10
    @Override
11
    public void addCorsMappings(CorsRegistry registry) {
12
        registry.addMapping("/**");
13
    }
14
}
server/src/main/resources/Message_cs.properties
1
hello=Ahoj sv\u011Bte
server/src/main/resources/Message_en.properties
1
hello=Hello world
server/src/main/resources/application.properties
1
spring.datasource.url=jdbc:mariadb://db:3306/yamanager
2
spring.datasource.username=user
3
spring.datasource.password=passwd
4
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
server/src/test/java/cz/zcu/yamanager/util/localization/MessageTest.java
1
package cz.zcu.yamanager.util.localization;
2

  
3
import org.junit.jupiter.api.Test;
4

  
5
import static org.junit.jupiter.api.Assertions.*;
6

  
7
class MessageTest {
8

  
9
    @Test
10
    void getString_inCzech_true() {
11
        Message.config().addLanguage(Language.CZ);
12

  
13
        assertEquals("český jazyk", Message.getString(Language.CZ, "hello"));
14
    }
15

  
16
    @Test
17
    void getString_inEnglish_true() {
18
        Message.config().addLanguage(Language.EN);
19

  
20
        assertEquals("english language", Message.getString(Language.EN, "hello"));
21
    }
22

  
23
    @Test
24
    void getString_general_true() {
25
        assertEquals("english language", Message.getString("hello"));
26
    }
27

  
28
    @Test
29
    void getString_multiple_true() {
30
        Message.config()
31
                .addLanguage(Language.CZ)
32
                .addLanguage(Language.EN);
33

  
34
        assertEquals("český jazyk", Message.getString(Language.CZ, "hello"));
35
        assertEquals("english language", Message.getString(Language.EN, "hello"));
36
        assertEquals("english language", Message.getString("hello"));
37
    }
38

  
39
    @Test
40
    void getString_missingSource_true() {
41
        assertEquals("CZ SOURCE MISSING", Message.getString(Language.CZ, "hello"));
42
    }
43
}
server/src/test/java/cz/zcu/yamanager/ws/rest/TestControllerTest.java
1
package cz.zcu.yamanager.ws.rest;
2

  
3
import cz.zcu.yamanager.util.localization.Language;
4
import cz.zcu.yamanager.util.localization.Message;
5
import org.junit.jupiter.api.BeforeEach;
6
import org.junit.jupiter.api.Test;
7

  
8
import static org.junit.jupiter.api.Assertions.assertEquals;
9

  
10
class TestControllerTest {
11

  
12
    private TestController testController;
13

  
14
    @BeforeEach
15
    void setUp() {
16
        Message.config()
17
                .addLanguage(Language.EN)
18
                .addLanguage(Language.CZ);
19

  
20
        testController = new TestController(null);
21
    }
22

  
23
    @Test
24
    void hello_inCzech_true() {
25
        assertEquals("český jazyk", testController.hello("cz"));
26
    }
27

  
28
    @Test
29
    void hello_inEnglish_true() {
... Rozdílový soubor je zkrácen, protože jeho délka přesahuje max. limit.

Také k dispozici: Unified diff