Skip to content

Commit

Permalink
back
Browse files Browse the repository at this point in the history
  • Loading branch information
sara-gaballa committed Dec 26, 2022
1 parent 91b07ee commit 28ee2aa
Show file tree
Hide file tree
Showing 14 changed files with 288 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"id" : "69c58548-c9f7-411f-87f9-c8f2aefac48b",
"from" : "[email protected]",
"to" : "[email protected]",
"date" : "12/26/22",
"time" : "8:31",
"subject" : "test",
"body" : "did that arrive?",
"priority" : null,
"attachments" : null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"id" : "69c58548-c9f7-411f-87f9-c8f2aefac48b",
"from" : "[email protected]",
"to" : "[email protected]",
"date" : "12/26/22",
"time" : "8:31",
"subject" : "test",
"body" : "did that arrive?",
"priority" : null,
"attachments" : null
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
package com.example.email.controller;

import com.example.email.model.Email;
import com.example.email.model.User;
import com.example.email.service.Logging;
import com.example.email.service.LoggingProxy;
import com.example.email.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.PriorityQueue;
import java.util.Queue;

@RestController
@CrossOrigin("https://localhost:4200")
@RequestMapping("/mail")
public class MailController {
@Autowired
MailService service = new MailService();
Logging logging = new LoggingProxy();

@RequestMapping("/signUp")
Expand All @@ -24,4 +32,20 @@ public void signUp() {
}
System.out.println(mockUser.getEmail());
}

@RequestMapping("/send")
public void send() {
User mockUser = logging.findUser("[email protected]");
Queue<String> q = new PriorityQueue<>();
q.add("[email protected]");
Email email = new Email("[email protected]", "[email protected]", "12/26/22", "8:31", "test",
"did that arrive?", null, null);
try {
this.service.sendMail(mockUser, email, q);
} catch (Exception e) {
}
System.out.println(mockUser.getEmail());
}
// sign in / send / add folder / rename folder/ delete folder/ delete mails/ get all mails / move mails

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
import java.util.List;
import java.util.Queue;

public class DataManager implements Manager {
// draft class
public class DataManager {
private final static String parentFolder = "parent";
private static ObjectMapper objectMapper = new ObjectMapper();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.example.email.mailmanager;

import com.example.email.model.Email;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FileAdapter implements MailManager {
private static ObjectMapper objectMapper = new ObjectMapper();

// return all mails in specific folder
@Override
public List<Email> getAllMails(String path) throws IOException {
File[] files = FileManager.getAllFiles(path);
List<Email> emails = new ArrayList<>();
for (File file : files) {
emails.add(objectMapper.readValue(file, Email.class));
}
return emails;
}

// return one mail with given folder and name
@Override
public Email getMail(String path, String name) throws IOException {
File file = FileManager.getFile(path, name);
return objectMapper.readValue(file, Email.class);
}

// delete all mails with the given named from the given folder
@Override
public void deleteMails(String path, String[] fileNames) {
for (String name : fileNames) {
name = name.concat(".json");
}
FileManager.deleteFiles(path, fileNames);
}

// add mail to the given path
@Override
public void addMail(String path, String fileName, Email email) throws IOException {
File file = FileManager.addFile(path, fileName + ".json");
//configure objectMapper for pretty input
objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);

//write email object to json file
objectMapper.writeValue(file, email);
}

// move mails from one folder to another
@Override
public void moveMails(String fromPath, String toPath, String[] fileNames) {
for (String name : fileNames) {
name = name.concat(".json");
}
FileManager.moveFiles(fromPath, toPath, fileNames);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.example.email.mailmanager;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.IOException;

public class FileManager {
private final static String parentFolder = "parent";
private static ObjectMapper objectMapper = new ObjectMapper();

//create new folder in the given path
public static boolean addFolder(String path) {
File folder = new File(parentFolder + "/" + path);
return folder.mkdirs();
}

//delete folder with the given path
public static boolean deleteFolder(String path) {
File folder = new File(parentFolder + "/" + path);
for (File file : folder.listFiles()) {
// delete files and empty sub-folders
file.delete();
}
return folder.delete();
}

//rename folder with the given path
public static boolean renameFolder(String oldPath, String newPath) {
File folder = new File(parentFolder + "/" + oldPath);
return folder.renameTo(new File(parentFolder + "/" + newPath));
}

public static File[] getAllFiles(String path) {
File file = new File(parentFolder + "/" + path);
return file.listFiles();
}

public static File getFile(String path, String name) {
File file = new File(parentFolder + "/" + path + "/" + name);
return file;
}

// delete files move them to trash folder
public static void deleteFiles(String path, String[] fileNames) {
String folder = parentFolder + "/" + path;
moveFiles(folder, parentFolder + "/" + FoldersName.TRASH, fileNames);
/*List<File> deletedFiles = new ArrayList<>();
for (String name : fileNames) {
File file = new File(folder + "/" + name);
deletedFiles.add(file);
file.delete();
}
return deletedFiles;*/
}

public static File addFile(String path, String fileName) throws IOException {
addFolder(path);
File file = new File(parentFolder + "/" + path + "/" + fileName);
file.createNewFile();
return file;
}

public static File[] moveFiles(String fromPath, String toPath, String[] fileNames) {
String fromFolder = parentFolder + "/" + fromPath;
String toFolder = parentFolder + "/" + toPath;
for (String name : fileNames) {
File file = new File(fromFolder + "/" + name);
file.renameTo(new File(toFolder + "/" + name));
}
return new File(toFolder).listFiles();
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.example.email.mailmanager;

class FoldersName {
public class FoldersName {
public static final String INBOX = "inbox";
public static final String SENT = "sent";
public static final String TRASH = "trash";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.example.email.mailmanager;

import com.example.email.model.Email;

import java.io.IOException;
import java.util.List;

public interface MailManager {//TODO: write email
// TODO : get folders on sign in

// get all emails from inbox folder
//public List<Email> getAllMails(User user) throws IOException;

// get all emails in specific folder
List<Email> getAllMails(String path) throws IOException;

// Check: Queue -> Array
Email getMail(String path, String name) throws IOException;

void addMail(String path, String fileName, Email email) throws IOException;

// TODO : add ID
void deleteMails(String path, String[] ids);

void moveMails(String fromPath, String toPath, String[] ids);
}
11 changes: 8 additions & 3 deletions back-end/src/main/java/com/example/email/model/Email.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.example.email.model;

public class Email {
private final String id;
private String id;
private String from;
private String to;
private String date;
Expand All @@ -11,9 +11,8 @@ public class Email {
private Priority priority;
private String[] attachments;

public Email(String id, String from, String to, String date, String time, String subject,
public Email(String from, String to, String date, String time, String subject,
String body, Priority priority, String[] attachments) {
this.id = id;
this.from = from;
this.to = to;
this.date = date;
Expand All @@ -28,6 +27,12 @@ public String getId() {
return id;
}

// set id only one time
public void setId(String id) {
if (this.id == null)
this.id = id;
}

public String getFrom() {
return from;
}
Expand Down
4 changes: 3 additions & 1 deletion back-end/src/main/java/com/example/email/model/User.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.example.email.model;

import com.example.email.mailmanager.FileManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -18,7 +20,7 @@ public User(String firstName, String lastName, String email, String password) {
this.email = email;
this.password = password;
this.contacts = new ArrayList<>();

FileManager.addFolder(email);
}

public String getFirstName() {
Expand Down
5 changes: 3 additions & 2 deletions back-end/src/main/java/com/example/email/service/Logging.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import java.util.List;

public interface Logging {
public void signUp(User user) throws IOException;
void signUp(User user) throws IOException;

public List<String> signIn(String email, String password);
List<String> signIn(String email, String password);

User findUser(String email);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ public List<String> signIn(String email, String password) {
throw new RuntimeException("Incorrect password");
return this.service.signIn(email, password);
}

@Override
public User findUser(String email) {
return service.findUser(email);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package com.example.email.service;

import com.example.email.model.User;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class LoggingService implements Logging {
Expand All @@ -19,17 +15,7 @@ public LoggingService() {

@Override
public void signUp(User user) throws IOException {
accounts.add(user);
System.out.println(Arrays.toString(accounts.toArray()));
File mailFolder = new File(user.getEmail() + ".json");
mailFolder.createNewFile();
ObjectMapper objectMapper = new ObjectMapper();

//configure objectMapper for pretty input
objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);

//write email object to json file
objectMapper.writeValue(mailFolder, user);
accounts.add(new User(user.getFirstName(), user.getLastName(), user.getEmail(), user.getPassword()));
}

@Override
Expand Down
Loading

0 comments on commit 28ee2aa

Please sign in to comment.