Skip to content
This repository has been archived by the owner on Dec 5, 2023. It is now read-only.

Commit

Permalink
✨ migrate to mongo
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouhao committed Apr 29, 2023
1 parent a67b1af commit 002ffb0
Show file tree
Hide file tree
Showing 31 changed files with 132 additions and 349 deletions.
23 changes: 13 additions & 10 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
version: "3.8"

services:
mysql:
image: mariadb:10.4
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: saltynote
MYSQL_ROOT_PASSWORD: password
volumes:
- mysql_data:/var/lib/mysql/data

redis:
image: 'redis:7.0-alpine'
command: redis-server --requirepass 88888888
ports:
- '6379:6379'

mongodb:
image: mongo:6-jammy
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: saltynote
MONGO_INITDB_ROOT_PASSWORD: password
MONGO_INITDB_DATABASE: saltynote
ports:
- '27017:27017'
volumes:
- mongo_data:/data/db

volumes:
mysql_data:
mongo_data:
18 changes: 5 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
Expand Down Expand Up @@ -84,19 +89,6 @@
<artifactId>freemarker</artifactId>
</dependency>

<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-mysql</artifactId>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>3.1.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/saltynote/service/ServiceApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
@EnableCaching
public class ServiceApplication {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.saltynote.service.configure;

import com.saltynote.service.generator.IdGenerator;
import com.saltynote.service.generator.SnowflakeIdGenerator;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
Expand All @@ -15,9 +12,4 @@ public class ServiceConfig {
@Value("${saltynote.machine.id}")
private int machineId;

@Bean
public IdGenerator snowflakeIdGenerator() {
return new SnowflakeIdGenerator(datacenterId, machineId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ public NoteController(NoteService noteService, NoteConverter noteConverter) {
}

@GetMapping("/note/{id}")
public ResponseEntity<Note> getNoteById(@PathVariable("id") Long id, Authentication auth) {
public ResponseEntity<Note> getNoteById(@PathVariable("id") String id, Authentication auth) {
Optional<Note> note = noteService.getById(id);
checkNoteOwner(note, auth);
return note.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
}

@RequestMapping(value = "/note/{id}", method = { RequestMethod.POST, RequestMethod.PUT })
public ResponseEntity<Note> updateNoteById(@PathVariable("id") Long id, @RequestBody NoteDto noteDto,
public ResponseEntity<Note> updateNoteById(@PathVariable("id") String id, @RequestBody NoteDto noteDto,
Authentication auth) {
Optional<Note> queryNote = noteService.getById(id);
checkNoteOwner(queryNote, auth);
Expand All @@ -59,9 +59,7 @@ public ResponseEntity<Note> updateNoteById(@PathVariable("id") Long id, @Request
noteTobeUpdate.setNote(noteDto.getNote());
}

if (StringUtils.isNotBlank(noteDto.getTags())) {
noteTobeUpdate.setTags(noteDto.getTags());
}
noteTobeUpdate.setTags(noteDto.getTags());

if (StringUtils.isNotBlank(noteDto.getHighlightColor())) {
noteTobeUpdate.setHighlightColor(noteDto.getHighlightColor());
Expand All @@ -71,7 +69,7 @@ public ResponseEntity<Note> updateNoteById(@PathVariable("id") Long id, @Request
}

@DeleteMapping("/note/{id}")
public ResponseEntity<ServiceResponse> deleteNoteById(@PathVariable("id") Long id, Authentication auth) {
public ResponseEntity<ServiceResponse> deleteNoteById(@PathVariable("id") String id, Authentication auth) {
Optional<Note> note = noteService.getById(id);
checkNoteOwner(note, auth);
noteService.delete(note.get());
Expand All @@ -82,7 +80,7 @@ public ResponseEntity<ServiceResponse> deleteNoteById(@PathVariable("id") Long i
// requests will be
// blocked by Chrome. Further investigation is required from me for this issue.
@PostMapping("/note/{id}/delete")
public ResponseEntity<ServiceResponse> postDeleteNoteById(@PathVariable("id") Long id, Authentication auth) {
public ResponseEntity<ServiceResponse> postDeleteNoteById(@PathVariable("id") String id, Authentication auth) {
return deleteNoteById(id, auth);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public ResponseEntity<JwtUser> signup(@Valid @RequestBody UserNewRequest userNew
SiteUser user = userNewRequest.toSiteUser();
user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
user = userService.create(user);
if (user.getId() != null && user.getId() > 0) {
if (user.getId() != null && !user.getId().isBlank()) {
vaultService.deleteById(vaultOp.get().getId());
return ResponseEntity.ok(new JwtUser(user.getId(), user.getUsername()));
}
Expand Down Expand Up @@ -199,7 +199,7 @@ public ResponseEntity<ServiceResponse> updatePassword(@Valid @RequestBody Passwo
}

@DeleteMapping("/account/{id}")
public ResponseEntity<ServiceResponse> accountDeletion(@PathVariable("id") Long userId, Authentication auth) {
public ResponseEntity<ServiceResponse> accountDeletion(@PathVariable("id") String userId, Authentication auth) {
JwtUser jwtUser = (JwtUser) auth.getPrincipal();
if (!Objects.equals(userId, jwtUser.getId())) {
throw new WebAppRuntimeException(HttpStatus.BAD_REQUEST, "User information is not confirmed");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

public interface Identifiable {

Long getId();
String getId();

}
2 changes: 1 addition & 1 deletion src/main/java/com/saltynote/service/domain/LoginUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public class LoginUser extends User implements IdentifiableUser {

@Getter
private final Long id;
private final String id;

public LoginUser(SiteUser user) {
super(user.getUsername(), user.getPassword(), Collections.emptyList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@Accessors(chain = true)
public class VaultEntity {

private Long userId;
private String userId;

private String secret;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@NoArgsConstructor
public class JwtUser implements IdentifiableUser {

private Long id;
private String id;

private String username;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.Set;

@Data
@Accessors(chain = true)
@JsonIgnoreProperties(ignoreUnknown = true)
public class NoteDto {

private Long userId;
private String userId;

@NotBlank
private String text;
Expand All @@ -26,6 +28,6 @@ public class NoteDto {

private String highlightColor;

private String tags;
private Set<String> tags;

}
36 changes: 7 additions & 29 deletions src/main/java/com/saltynote/service/entity/LoginHistory.java
Original file line number Diff line number Diff line change
@@ -1,50 +1,28 @@
package com.saltynote.service.entity;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.PrePersist;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import java.sql.Timestamp;
import java.util.Date;

@Entity
@Table(name = "login_history")
@Document
@Getter
@Setter
@Accessors(chain = true)
public class LoginHistory {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Integer id;
private String id;

@Column(name = "user_id")
private Long userId;
private String userId;

@Size(max = 128)
@Column(name = "remote_ip", length = 128)
private String remoteIp;

@Size(max = 256)
@Column(name = "user_agent", length = 256)
private String userAgent;

@NotNull
@Column(name = "login_time", nullable = false)
private Timestamp loginTime;

@PrePersist
private void beforeSave() {
this.loginTime = new Timestamp(System.currentTimeMillis());
}
private Date loginTime = new Date();

}
48 changes: 11 additions & 37 deletions src/main/java/com/saltynote/service/entity/Note.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
package com.saltynote.service.entity;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.saltynote.service.domain.Identifiable;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.PrePersist;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotBlank;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.springframework.util.StringUtils;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import java.io.Serial;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Objects;
import java.util.Set;

@Entity
@Table(name = "note")
@Document
@Getter
@Setter
@ToString
Expand All @@ -33,46 +28,25 @@ public class Note implements Serializable, Identifiable {
private static final long serialVersionUID = 1L;

@Id
@Column(name = "id")
private Long id;
private String id;

@Column(name = "user_id")
private Long userId;
private String userId;

@Column(name = "text", nullable = false)
@NotBlank
private String text;

@Column(name = "url", nullable = false)
@NotBlank
private String url;

@Column(name = "note")
private String note;

@Column(name = "is_page_only")
@JsonProperty("is_page_only")
private Boolean pageOnly;
private Boolean isPageOnly = false;

@Column(name = "highlight_color")
private String highlightColor;
private String highlightColor = "";

@Column(name = "created_time", nullable = false)
private Timestamp createdTime;
private Date createdTime = new Date();

@Column(name = "tags")
private String tags;

@PrePersist
private void beforeSave() {
this.createdTime = new Timestamp(System.currentTimeMillis());
if (this.pageOnly == null) {
this.pageOnly = false;
}
if (!StringUtils.hasText(this.highlightColor)) {
this.highlightColor = "";
}
}
private Set<String> tags;

@Override
public boolean equals(Object o) {
Expand Down
Loading

0 comments on commit 002ffb0

Please sign in to comment.