Skip to content

Commit

Permalink
Make repository structure for backend.
Browse files Browse the repository at this point in the history
  • Loading branch information
gavrilovegor519 committed Feb 27, 2024
1 parent 0f9ba9c commit 84b8817
Show file tree
Hide file tree
Showing 16 changed files with 358 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.egor.rssaggregator.dto.input;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.Builder;
import lombok.Data;

@Data
@Builder
public class AddFeedDto {
@NotBlank
@Size(max = 50)
private String name;

@NotBlank
@Size(max = 250)
private String url;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.egor.rssaggregator.dto.input;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.Builder;
import lombok.Data;

@Data
@Builder
public class LoginDto {
@NotBlank
@Size(max = 50)
private String email;

@NotBlank
@Size(max = 32)
private String password;
}
18 changes: 18 additions & 0 deletions backend/src/main/java/com/egor/rssaggregator/dto/input/RegDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.egor.rssaggregator.dto.input;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.Builder;
import lombok.Data;

@Data
@Builder
public class RegDto {
@NotBlank
@Size(max = 50)
private String email;

@NotBlank
@Size(max = 32)
private String password;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.egor.rssaggregator.dto.output;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.Builder;
import lombok.Data;

@Data
@Builder
public class GetFeedDto {
@NotBlank
@Size(max = 50)
private String name;

@NotBlank
@Size(max = 250)
private String url;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.egor.rssaggregator.dto.output;

import com.egor.rssaggregator.entity.Feed;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Builder;
import lombok.Data;

import java.time.LocalDate;

@Data
@Builder
public class MainNewsEntryDto {
@NotBlank
@Size(max = 100)
private String newsHead;

@NotNull
private LocalDate newsDate;

@NotNull
private Feed source;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.egor.rssaggregator.dto.output;

import com.egor.rssaggregator.entity.Feed;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Builder;
import lombok.Data;

import java.time.LocalDate;

@Data
@Builder
public class MoreTextNewsEntryDto {
@NotBlank
@Size(max = 100)
private String newsHead;

@NotNull
private LocalDate newsDate;

private String newsText;

@NotNull
private Feed source;
}
48 changes: 48 additions & 0 deletions backend/src/main/java/com/egor/rssaggregator/entity/Feed.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.egor.rssaggregator.entity;

import jakarta.persistence.*;
import lombok.*;
import org.hibernate.proxy.HibernateProxy;

import java.util.Objects;

@Entity
@Table(name = "feeds")
@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Feed {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Long id;

@Column(name = "name", unique = true, length = 50, nullable = false)
private String name;

@Column(name = "url", unique = true, length = 250, nullable = false)
private String url;

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "user_id")
private User user;

@Override
public final boolean equals(Object o) {
if (this == o) return true;
if (o == null) return false;
Class<?> oEffectiveClass = o instanceof HibernateProxy ? ((HibernateProxy) o).getHibernateLazyInitializer().getPersistentClass() : o.getClass();
Class<?> thisEffectiveClass = this instanceof HibernateProxy ? ((HibernateProxy) this).getHibernateLazyInitializer().getPersistentClass() : this.getClass();
if (thisEffectiveClass != oEffectiveClass) return false;
Feed feed = (Feed) o;
return getId() != null && Objects.equals(getId(), feed.getId());
}

@Override
public final int hashCode() {
return this instanceof HibernateProxy ? ((HibernateProxy) this).getHibernateLazyInitializer().getPersistentClass().hashCode() : getClass().hashCode();
}
}
53 changes: 53 additions & 0 deletions backend/src/main/java/com/egor/rssaggregator/entity/News.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.egor.rssaggregator.entity;

import jakarta.persistence.*;
import lombok.*;

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

@Entity
@Table(name = "news")
@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class News {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Long id;

@Column(name = "newsHead", length = 100, nullable = false)
private String newsHead;

@Column(name = "newsDate", nullable = false)
private LocalDate newsDate;

@Column(name = "newsText")
@Lob
private String newsText;

@ToString.Exclude
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "feed_id")
private Feed feed;

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
News news = (News) o;
return Objects.equals(id, news.id) &&
Objects.equals(newsHead, news.newsHead) &&
Objects.equals(newsDate, news.newsDate) &&
Objects.equals(feed, news.feed);
}

@Override
public int hashCode() {
return Objects.hash(id, newsHead, newsDate, feed);
}
}
83 changes: 83 additions & 0 deletions backend/src/main/java/com/egor/rssaggregator/entity/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.egor.rssaggregator.entity;

import jakarta.persistence.*;
import lombok.*;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.*;

@Entity
@Table(name = "users")
@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class User implements UserDetails {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Long id;

@Column(name = "email", unique = true, length = 50, nullable = false)
private String email;

@Column(name = "password", length = 120, nullable = false)
@ToString.Exclude
private String password;

@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
@ToString.Exclude
private List<Feed> feeds = new ArrayList<>();

@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
var userRole = new HashSet<SimpleGrantedAuthority>();
userRole.add(new SimpleGrantedAuthority("ROLE_USER")); // all users are users, not admins
return userRole;
}

@Override
public String getUsername() {
return email;
}

@Override
public boolean isAccountNonExpired() {
return true;
}

@Override
public boolean isAccountNonLocked() {
return true;
}

@Override
public boolean isCredentialsNonExpired() {
return true;
}

@Override
public boolean isEnabled() {
return true;
}

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

@Override
public int hashCode() {
return Objects.hash(id, email, password, feeds);
}
}
10 changes: 10 additions & 0 deletions backend/src/main/java/com/egor/rssaggregator/repo/FeedRepo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.egor.rssaggregator.repo;

import com.egor.rssaggregator.entity.Feed;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface FeedRepo extends JpaRepository<Feed, Long> {
Optional<Feed> findById(long id);
}
10 changes: 10 additions & 0 deletions backend/src/main/java/com/egor/rssaggregator/repo/NewsRepo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.egor.rssaggregator.repo;

import com.egor.rssaggregator.entity.News;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface NewsRepo extends JpaRepository<News, Long> {
Optional<News> findById(long id);
}
12 changes: 12 additions & 0 deletions backend/src/main/java/com/egor/rssaggregator/repo/UserRepo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.egor.rssaggregator.repo;

import com.egor.rssaggregator.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface UserRepo extends JpaRepository<User, Long> {
Optional<User> findByEmail(String email);

Optional<User> findById(long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.egor.rssaggregator.service;

public interface FeedService {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.egor.rssaggregator.service;

public interface UserService {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.egor.rssaggregator.service.impl;

import com.egor.rssaggregator.service.FeedService;

public class FeedServiceImpl implements FeedService {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.egor.rssaggregator.service.impl;

import com.egor.rssaggregator.service.UserService;

public class UserServiceImpl implements UserService {
}

0 comments on commit 84b8817

Please sign in to comment.