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
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ public class Folder {
private Date CreatedDate;
private Date modifiedDate;

@ManyToOne(fetch = FetchType.LAZY, optional = false)
@ManyToOne(
optional = false,
cascade = CascadeType.ALL
)
@OnDelete(action = OnDeleteAction.CASCADE)
@JoinColumn(
name = "user_id",
referencedColumnName = "id"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.barmjz.productivityapp.Note;


import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -27,13 +26,13 @@ public ResponseEntity<Note> createNote(@RequestParam("folderId") Long folderId,
}

@PutMapping("/modify")
public ResponseEntity<String> modifyNote(@RequestParam("noteId") Long noteId, @RequestBody ObjectNode objectNode) {
public ResponseEntity<Note> modifyNote(@RequestParam("folderId") Long folderId, @RequestBody String modifiedNote) {
try {
String title = objectNode.get("title").asText();
String content = objectNode.get("content").asText();
return ResponseEntity.status(HttpStatus.OK).body(noteManager.modifyNote(noteId, title, content));
System.out.println(modifiedNote);
// return ResponseEntity.status(HttpStatus.OK).body(noteManager.modifyNote(modifiedNote, folderId));
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
} catch (Exception exception) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Fuck");
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,24 @@ public Note createNote(Long folderId, String noteTitle){
return newNote;
}

public String modifyNote(Long noteId, String title, String content){
if(noteId == null || !noteRepo.existsById(noteId))
public Note modifyNote(Note modifiedNote, Long folderId){
if(modifiedNote == null || modifiedNote.getId() == null)
throw new NullPointerException("note is null");
if(!noteRepo.existsById(modifiedNote.getId()))
throw new NoSuchElementException("note not found");
Note existedNote = noteRepo.getReferenceById(noteId);
if( (noteRepo.existsByTitleAndFolder_Id(title,existedNote.getFolder().getId())) &&
(!noteRepo.findByTitleAndFolder_Id(title,existedNote.getFolder().getId()).getId().equals(existedNote.getId())))
throw new IllegalStateException("note new title already exist");
existedNote.setTitle(title);
existedNote.setContent(content);
// if(!noteRepo.findByTitleAndFolder_Id(modifiedNote.getTitle(),folderId)
// .getId().equals(modifiedNote.getId()))
// throw new IllegalStateException("note title already exist");
Note existedNote = noteRepo.getReferenceById(modifiedNote.getId());
// need more efficient way
existedNote.setTitle(modifiedNote.getTitle());
existedNote.setContent(modifiedNote.getContent());
existedNote.setModifiedDate(new Date());
existedNote.setStarred(modifiedNote.isStarred());
existedNote.setColor(modifiedNote.getColor());
existedNote.setFontSize(modifiedNote.getFontSize());
noteRepo.save(existedNote);
return "note modified";
return existedNote;
}

public List<Note> getFolderNotes(Long folderId){
Expand All @@ -74,8 +80,8 @@ public List<Note> getUserStarredNotes(Long userId){
public Note moveNote(Long newFolderId, Long noteId){
if(noteId == null || newFolderId == null || !folderRepo.existsFolderById(newFolderId) || !noteRepo.existsById(noteId))
throw new NoSuchElementException("not found");
Note note = noteRepo.findById(noteId).get();
note.setFolder(folderRepo.findById(newFolderId).get());
Note note = noteRepo.getReferenceById(noteId);
note.setFolder(folderRepo.getReferenceById(newFolderId));
noteRepo.save(note);
return note;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
@AllArgsConstructor
@RestController
@RequestMapping("/api/category")
@CrossOrigin
public class CategoryController {

private final CategoryService categoryService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
Expand All @@ -18,8 +19,9 @@ public interface CategoryRepo extends JpaRepository<Category, Long> {

Optional<List<Category>> getCategoryByUserId(long userId);

@Modifying
@Query("UPDATE Category SET category_name = ?1 WHERE id = ?2")
@Modifying(clearAutomatically = true)
@Transactional
@Query("UPDATE Category c SET c.category_name = ?1 WHERE c.id = ?2")
void renameCategory(String categoryName, long id);

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
@Setter
@AllArgsConstructor
public class CategoryPair {
Category category;
long id;
String name;
List<Task> tasks;
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.barmjz.productivityapp.todo_task_category.task;
import com.barmjz.productivityapp.todo_task_category.category.Category;
import com.barmjz.productivityapp.user.User;
import jakarta.persistence.NamedStoredProcedureQuery;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
Expand All @@ -20,15 +22,18 @@ public interface OneTimeTaskRepo extends JpaRepository<OneTimeTask, Long> {


Optional<List<OneTimeTask>> getTop10ByUserIdAndCompletionDateNotNullOrderByCompletionDateDesc(Long userId);

@Transactional
@Modifying(clearAutomatically = true)
@Query("UPDATE OneTimeTask t SET t.completionDate = ?2 WHERE t.id = ?1")
void markTaskAsDone(Long taskId, Date currentDate);

@Transactional

@Modifying(clearAutomatically = true)
@Query("UPDATE OneTimeTask t SET t.completionDate = null WHERE t.id = ?1")
void unMarkTaskAsDone(Long taskId);

@Transactional
@Modifying(clearAutomatically = true)
@Query("UPDATE OneTimeTask t SET t.todo = ?2 WHERE t.id = ?1")
void changeTodoFlag(Long taskId, boolean bool);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public class Task extends AbstractTask {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
@JsonIgnore
private Date creationDate;

@ManyToOne(fetch = FetchType.LAZY)
Expand All @@ -29,7 +28,6 @@ public class Task extends AbstractTask {

@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "userId", referencedColumnName = "id")
@JsonIgnore
private User user;

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
@AllArgsConstructor
@RestController
@RequestMapping("/api/task")
@CrossOrigin
public class TaskController {

private final TaskService taskService;
Expand Down Expand Up @@ -46,7 +47,7 @@ public ResponseEntity<String> deleteTask(@PathVariable Long taskId) {
}

@PostMapping("/")
public ResponseEntity<Task> createTask(@RequestBody Task task, @RequestParam String taskType) {
public ResponseEntity<Task> createTask(@RequestBody Object task, @RequestParam String taskType) {
try {
return ResponseEntity.ok(taskService.createTask(task, taskType));
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,24 @@ public void deleteTask(Long taskId){
repeatedTaskRepo.deleteById(taskId);
}

public Task createTask(Task task, String taskType){
public Task createTask(Object task, String taskType){
Task newTask;
if (taskType.equals("onetime")) {
OneTimeTask parsedOnetimeTask = (OneTimeTask) task;
oneTimeTaskRepo.save((OneTimeTask) task);
newTask = oneTimeTaskRepo.getByCreationDate(task.getCreationDate()).orElse(null);
newTask = oneTimeTaskRepo.getByCreationDate(parsedOnetimeTask.getCreationDate()).orElse(null);
} else {
repeatedTaskRepo.save((RepeatedTask) task);
newTask = repeatedTaskRepo.getByCreationDate(task.getCreationDate()).orElse(null);
RepeatedTask parsedOnetimeTask = (RepeatedTask) task;
newTask = repeatedTaskRepo.getByCreationDate(parsedOnetimeTask.getCreationDate()).orElse(null);
}
return newTask;
}

public Task tickTask(Long taskId, Long date, String taskType){
Task newTask;
Date currentDate = new Date(date);
User user = userRepo.getUserByEmail(userAuthentication.getName()).orElse(null);
User user = userRepo.getUserByEmail(SecurityContextHolder.getContext().getAuthentication().getName()).orElse(null);
if (taskType.equals("onetime")) {
oneTimeTaskRepo.markTaskAsDone(taskId, currentDate);
newTask = oneTimeTaskRepo.findById(taskId).get();
Expand Down Expand Up @@ -122,15 +124,15 @@ public Task untickTask(Long taskId, Long date){
}

public List<Task> getCompletedTasks(){
User user = userRepo.getUserByEmail(userAuthentication.getName()).orElse(null);
User user = userRepo.getUserByEmail(SecurityContextHolder.getContext().getAuthentication().getName()).orElse(null);
List<OneTimeTask> completedOneTimeTasks = oneTimeTaskRepo.getCompletedTasks(user).orElse(null);
assert completedOneTimeTasks != null;
return new ArrayList<>(completedOneTimeTasks);
}


public List<CategoryPair> getCategorizedTasks(){
String user = userAuthentication.getName();
String user = SecurityContextHolder.getContext().getAuthentication().getName();
List<CategoryPair> categoryTaskPairs = new ArrayList<>();
List<Category> categories = categoryRepo
.getCategoryByUserId(
Expand All @@ -146,7 +148,7 @@ public List<CategoryPair> getCategorizedTasks(){
categoryTasks.addAll(repeatedTaskRepo
.getAllByCategory(category)
.get());
categoryTaskPairs.add(new CategoryPair(category, categoryTasks));
categoryTaskPairs.add(new CategoryPair(category.getId(), category.getCategory_name(), categoryTasks));
}
return categoryTaskPairs;
}
Expand Down
2 changes: 1 addition & 1 deletion backend/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL55Dialect
rsa.private-key=classpath:certs/private.pem
rsa.public-key=classpath:certs/public.pem
rsa.public-key=classpath:certs/public.pem
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ void getExistedUserFolders(){

@Test
void getNonExistedUserFolders(){
userRepo.save(user1);
folderRepo.save(folder1);
folderRepo.save(folder2);
assertThatThrownBy(() -> folderManager.getUserFolders(user1.getId()+3))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.barmjz.productivityapp.Folder.Folder;
import com.barmjz.productivityapp.Folder.FolderRepo;
import com.barmjz.productivityapp.user.User;
import com.barmjz.productivityapp.user.UserRepo;
import lombok.Data;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -20,11 +19,9 @@

@DataJpaTest
class CreateNoteTest {
@Autowired UserRepo userRepo;
@Autowired FolderRepo folderRepo;
@Autowired NoteRepo noteRepo;
NoteManager noteManager;
User user;
Folder folder;
Note note1;
Note note2;
Expand All @@ -36,16 +33,14 @@ void setUp() {
folderRepo.deleteAll();
noteRepo.deleteAll();
date = new Date();
user = User.builder()
.email("user1@gmail.com")
.password("pass")
.firstName("userFirst")
.lastName("userLast")
.build();
userRepo.save(user);
folder = Folder.builder()
.name("folder")
.user(user)
.user(User.builder()
.email("user1@gmail.com")
.password("pass")
.firstName("userFirst")
.lastName("userLast")
.build())
.CreatedDate(date)
.modifiedDate(date)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ void getStarredNotes(){
note2 = noteManager.createNote(folder2.getId(),"note2");
note2.setContent("content2");
noteManager.alterStar(note2.getId());
noteManager.modifyNote(note2.getId(), note2.getTitle(), note2.getContent());
noteManager.modifyNote(note2, note2.getFolder().getId());
assertThat(note2.isStarred()).isTrue();
assertThat(noteRepo.findById(note2.getId()).get().getContent()).isEqualTo("content2");
List<Note> starredNotes = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.barmjz.productivityapp.Folder.Folder;
import com.barmjz.productivityapp.Folder.FolderRepo;
import com.barmjz.productivityapp.user.User;
import com.barmjz.productivityapp.user.UserRepo;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -19,8 +18,8 @@
@DataJpaTest
class ModifyNoteTest {

@Autowired UserRepo userRepo;
@Autowired FolderRepo folderRepo;
@Autowired
FolderRepo folderRepo;
@Autowired NoteRepo noteRepo;
NoteManager noteManager;
Folder folder;
Expand All @@ -34,16 +33,14 @@ void setUp() {
folderRepo.deleteAll();
noteRepo.deleteAll();
date = new Date();
User user = User.builder()
.email("user1@gmail.com")
.password("pass")
.firstName("userFirst")
.lastName("userLast")
.build();
userRepo.save(user);
folder = Folder.builder()
.name("folder")
.user(user)
.user(User.builder()
.email("user1@gmail.com")
.password("pass")
.firstName("userFirst")
.lastName("userLast")
.build())
.CreatedDate(date)
.modifiedDate(date)
.build();
Expand All @@ -61,16 +58,19 @@ void modifyNote(){
.folder(folder)
.createdDate(date)
.modifiedDate(date)
.fontSize(8)
.build();
noteManager.modifyNote(note2.getId(), note2.getTitle(), note2.getContent());
noteManager.modifyNote(note2, folder.getId());
assertThat(noteRepo.findById(note1.getId()).get().getContent()).isEqualTo("note 1 content");
assertThat(noteRepo.findById(note1.getId()).get().getFontSize()).isEqualTo(8);
// assertThat(noteRepo.findById(note1.getId()).get()).isEqualTo(note2);
}

@Test
void invalidModifiedNote(){
assertThatThrownBy(() -> noteManager.modifyNote(null, "new title", "new content"))
.isInstanceOf(NoSuchElementException.class)
.hasMessageContaining("note not found");
assertThatThrownBy(() -> noteManager.modifyNote(null, folder.getId()))
.isInstanceOf(NullPointerException.class)
.hasMessageContaining("note is null");
}

@Test
Expand All @@ -85,8 +85,9 @@ void modifyNonExistedNote(){
.folder(folder)
.createdDate(date)
.modifiedDate(date)
.fontSize(8)
.build();
assertThatThrownBy(() -> noteManager.modifyNote(note2.getId(),note2.getTitle(), note2.getContent()))
assertThatThrownBy(() -> noteManager.modifyNote(note2,folder.getId()))
.isInstanceOf(NoSuchElementException.class)
.hasMessageContaining("note not found");
}
Expand Down
Loading