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

Meet spring spring boot and rest #19

Open
wants to merge 3 commits into
base: meet-spring_spring-boot-and-rest
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ public class CatsgramApplication {
public static void main(String[] args) {
SpringApplication.run(CatsgramApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
package ru.yandex.practicum.catsgram.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import ru.yandex.practicum.catsgram.model.Post;
import ru.yandex.practicum.catsgram.models.Post;
import java.util.ArrayList;
import java.util.List;

@RestController
public class PostController {

private static final Logger log = LoggerFactory.getLogger(PostController.class);
private final List<Post> posts = new ArrayList<>();

@GetMapping("/posts")
public List<Post> findAll() {
log.debug("Текущее количество постов: {}", posts.size());
return posts;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package ru.yandex.practicum.catsgram.controller;

import org.springframework.web.bind.annotation.*;
import ru.yandex.practicum.catsgram.models.User;
import ru.yandex.practicum.catsgram.exception.InvalidEmailException;
import ru.yandex.practicum.catsgram.exception.UserAlreadyExistException;

import java.util.*;

@RestController
@RequestMapping("/users")
public class UserController {
private final Map<String, User> users = new HashMap<>();

@GetMapping
public Collection<User> findAll() {
return users.values();
}

@PostMapping
public User create(@RequestBody User user) {
if(user.getEmail() == null || user.getEmail().isBlank()) {
throw new InvalidEmailException("Адрес электронной почты не может быть пустым.");
}
if(users.containsKey(user.getEmail())) {
throw new UserAlreadyExistException("Пользователь с электронной почтой " +
user.getEmail() + " уже зарегистрирован.");
}
users.put(user.getEmail(), user);
return user;
}

@PutMapping
public User put(@RequestBody User user) {
if(user.getEmail() == null || user.getEmail().isBlank()) {
throw new InvalidEmailException("Адрес электронной почты не может быть пустым.");
}
users.put(user.getEmail(), user);

return user;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ru.yandex.practicum.catsgram.exception;

public class InvalidEmailException extends RuntimeException {
public InvalidEmailException(String s) {
super(s);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ru.yandex.practicum.catsgram.exception;

public class UserAlreadyExistException extends RuntimeException {
public UserAlreadyExistException(String s) {
super(s);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.yandex.practicum.catsgram.model;
package ru.yandex.practicum.catsgram.models;

import java.time.Instant;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package ru.yandex.practicum.catsgram.models;

import java.time.LocalDate;
import java.util.Objects;

public class User {
private String email;
private String nickname;
private LocalDate birthdate;

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getNickname() {
return nickname;
}

public void setNickname(String nickname) {
this.nickname = nickname;
}

public LocalDate getBirthdate() {
return birthdate;
}

public void setBirthdate(LocalDate birthdate) {
this.birthdate = birthdate;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return email.equals(user.email);
}

@Override
public int hashCode() {
return Objects.hash(email);
}
}
2 changes: 2 additions & 0 deletions develop/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
logging.level.org.zalando.logbook=TRACE
logging.level.ru.yandex.practicum.contollers=debug
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package test.java.ru.yandex.practicum.catsgram;
package ru.yandex.practicum.catsgram;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
Expand Down
24 changes: 23 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.1</version>
<version>2.6.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>ru.yandex.practicum</groupId>
Expand All @@ -22,11 +22,33 @@
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
<groupId>org.zalando</groupId>
<artifactId>logbook-spring-boot-starter</artifactId>
<version>2.14.0</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.1</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
Expand Down
1 change: 0 additions & 1 deletion src/main/resources/application.properties

This file was deleted.