Skip to content
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# REST API using Spring Boot

## task 0
## task 1
36 changes: 36 additions & 0 deletions src/main/java/task/controller/NotificationController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package task.controller;

import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import task.model.Notification;
import task.model.Task;
import task.service.NotificationService;

import java.util.List;

@RestController
@RequestMapping("/api/notifications")
@AllArgsConstructor
public class NotificationController {
private final NotificationService notificationService;
@PostMapping
public ResponseEntity<Notification> createNotification(@RequestBody Notification notification) {
return new ResponseEntity<>(notificationService.createNotification(notification), HttpStatus.CREATED);
}
@GetMapping
public ResponseEntity<List<Notification>> getAllNotifications() {
return ResponseEntity.ok(notificationService.getAllNotifications());
}
@GetMapping("/user/{userId}")
public ResponseEntity<List<Notification>> getUserNotifications(@PathVariable Long userId) {
return ResponseEntity.ok(notificationService.getUserNotifications(userId));
}

@GetMapping("/user/{userId}/unread")
public ResponseEntity<List<Notification>> getUserNotificationsUnread(@PathVariable Long userId) {
return ResponseEntity.ok(notificationService.getUserNotificationsUnread(userId));
}

}
43 changes: 43 additions & 0 deletions src/main/java/task/controller/TaskController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package task.controller;

import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import task.model.Task;
import task.service.TaskService;

import java.util.List;

@RestController
@RequestMapping(path = "/api/tasks")
@AllArgsConstructor
public class TaskController {
private final TaskService taskService;

@GetMapping
public ResponseEntity<List<Task>> getAllTasks() {
return ResponseEntity.ok(taskService.getAllTasks());
}

@GetMapping("/user/{userId}")
public ResponseEntity<List<Task>> getUserTasks(@PathVariable Long userId) {
return ResponseEntity.ok(taskService.getUserTasks(userId));
}
@GetMapping("/user/{userId}/pending")
public ResponseEntity<List<Task>> getUserTasksPending(@PathVariable Long userId) {
return ResponseEntity.ok(taskService.getUserTasksPending(userId));
}


@PostMapping
public ResponseEntity<Task> createTask(@RequestBody Task task) {
return new ResponseEntity<>(taskService.createTask(task), HttpStatus.CREATED);
}

@DeleteMapping("/{taskId}")
public ResponseEntity<Void> deleteTask(@PathVariable Long taskId) {
taskService.deleteTask(taskId);
return ResponseEntity.noContent().build();
}
}
35 changes: 35 additions & 0 deletions src/main/java/task/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package task.controller;

import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import task.model.User;
import task.service.UserService;

import java.util.List;

@RestController
@RequestMapping("/api/users")
@AllArgsConstructor
public class UserController {
private final UserService userService;

@GetMapping
public ResponseEntity<List<User>> getAllUsers() {
return ResponseEntity.ok(userService.getAllUsers());
}

@PostMapping
public ResponseEntity<User> register(@RequestBody User user) {
return new ResponseEntity<>(userService.register(user), HttpStatus.CREATED);
}


@GetMapping("/{userId}")
public ResponseEntity<User> getUserById(@PathVariable Long userId) {
return userService.getUserById(userId)
.map(ResponseEntity::ok)
.orElseGet(() -> ResponseEntity.notFound().build());
}
}
30 changes: 30 additions & 0 deletions src/main/java/task/model/Notification.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package task.model;

import lombok.*;

import java.time.LocalDateTime;

@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Notification {
private Long notificationId;

@NonNull
private String text;

@NonNull
@Builder.Default
private LocalDateTime dateCreate = LocalDateTime.now();

@NonNull
private Long taskId;

@NonNull
private Long userId;

@NonNull
@Builder.Default
private Boolean isRead = false;
}
37 changes: 37 additions & 0 deletions src/main/java/task/model/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package task.model;

import lombok.*;


import java.time.LocalDateTime;

@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Task {

private Long taskId;

@NonNull
@Builder.Default
private String taskText = "New task";

private LocalDateTime actionDate;

@NonNull
@Builder.Default
private LocalDateTime createDate = LocalDateTime.now();

@NonNull
@Builder.Default
private Boolean isComplete = false;

@NonNull
private Long userId;

@NonNull
@Builder.Default
private Boolean isDelete = false;

}
18 changes: 18 additions & 0 deletions src/main/java/task/model/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package task.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Long userId;
private String login;
private String password;
private String userName;
private String email;
}
13 changes: 13 additions & 0 deletions src/main/java/task/repository/NotificationRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package task.repository;

import task.model.Notification;

import java.util.List;

public interface NotificationRepository {
public List<Notification> findAll();
public List<Notification> findByUserId(Long userId);
public List<Notification> findByUserIdAndIsReadFalse(Long userId);
public Notification save(Notification notification);

}
16 changes: 16 additions & 0 deletions src/main/java/task/repository/TaskRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package task.repository;

import task.model.Task;

import java.util.List;
import java.util.Optional;

public interface TaskRepository {

public List<Task> findAllIsDeleteFalse();
public List<Task> findByUserIdAndIsDeleteFalse(Long userId);
public List<Task> findByUserIdAndIsCompleteFalseAndIsDeleteFalse(Long userId) ;
public Task save(Task task) ;
public void markAsDeleted(Long taskId);

}
14 changes: 14 additions & 0 deletions src/main/java/task/repository/UserRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package task.repository;

import task.model.User;

import java.util.List;
import java.util.Optional;

public interface UserRepository {

public List<User> findAll();
public User save(User user);
public Optional<User> findByUserName(String userName);
public Optional<User> findById(Long userId);
}
46 changes: 46 additions & 0 deletions src/main/java/task/repository/impl/NotificationRepositoryImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package task.repository.impl;

import lombok.AllArgsConstructor;
import org.springframework.stereotype.Repository;
import task.model.Notification;
import task.model.User;
import task.repository.NotificationRepository;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;

@Repository
public class NotificationRepositoryImpl implements NotificationRepository {

private final ConcurrentHashMap<Long, Notification> notifications = new ConcurrentHashMap<>();
private final AtomicLong idCounter = new AtomicLong(1);
@Override
public List<Notification> findAll() {
return notifications.values().stream()
.collect(Collectors.toList());
}

@Override
public List<Notification> findByUserId(Long userId) {
return notifications.values().stream()
.filter(notification -> notification.getUserId().equals(userId))
.collect(Collectors.toList());
}
@Override
public List<Notification> findByUserIdAndIsReadFalse(Long userId) {
return notifications.values().stream()
.filter(notification -> notification.getUserId().equals(userId)
&& !notification.getIsRead())
.collect(Collectors.toList());
}
@Override
public Notification save(Notification notification) {
if (notification.getNotificationId() == null) {
notification.setNotificationId(idCounter.getAndIncrement());
}
notifications.put(notification.getNotificationId(), notification);
return notification;
}
}
54 changes: 54 additions & 0 deletions src/main/java/task/repository/impl/TaskRepositoryImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package task.repository.impl;

import lombok.AllArgsConstructor;
import org.springframework.stereotype.Repository;

import task.model.Task;
import task.repository.TaskRepository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;

@Repository
public class TaskRepositoryImpl implements TaskRepository {
private final ConcurrentHashMap<Long, Task> tasks = new ConcurrentHashMap<>();
private final AtomicLong idCounter = new AtomicLong(1);
@Override
public List<Task> findAllIsDeleteFalse() {
return tasks.values().stream()
.filter(task -> !task.getIsDelete())
.collect(Collectors.toList());
}
@Override
public List<Task> findByUserIdAndIsDeleteFalse(Long userId) {
return tasks.values().stream()
.filter(task -> task.getUserId().equals(userId) && !task.getIsDelete())
.collect(Collectors.toList());
}
@Override
public List<Task> findByUserIdAndIsCompleteFalseAndIsDeleteFalse(Long userId) {
return tasks.values().stream()
.filter(task -> task.getUserId().equals(userId)
&& !task.getIsComplete()
&& !task.getIsDelete())
.collect(Collectors.toList());
}
@Override
public Task save(Task task) {
if (task.getTaskId() == null) {
task.setTaskId(idCounter.getAndIncrement());
task.setCreateDate(LocalDateTime.now());
}
tasks.put(task.getTaskId(), task);
return task;
}
@Override
public void markAsDeleted(Long taskId) {
Optional.ofNullable(tasks.get(taskId))
.ifPresent(task -> task.setIsDelete(true));
}
}
Loading