Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not merge #38

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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 starter/cloudstorage/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ target/
.sts4-cache

### IntelliJ IDEA ###
.idea
.idea/
*.iws
*.iml
*.ipr
Expand Down
14 changes: 12 additions & 2 deletions starter/cloudstorage/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>

<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
Expand Down Expand Up @@ -86,7 +90,7 @@
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>3.8.1</version>
<version>5.5.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand All @@ -96,7 +100,13 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<org.slf4j.simpleLogger.defaultLogLevel>error</org.slf4j.simpleLogger.defaultLogLevel>
</systemPropertyVariables>
</configuration>
</plugin>

</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.udacity.jwdnd.course1.cloudstorage.controllers;

import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ErrController implements ErrorController {

@RequestMapping("/error")
public String handleError() {
return "error";
}

@Override
public String getErrorPath() {
return "/error";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.udacity.jwdnd.course1.cloudstorage.controllers;

import com.udacity.jwdnd.course1.cloudstorage.models.File;
import com.udacity.jwdnd.course1.cloudstorage.models.User;
import com.udacity.jwdnd.course1.cloudstorage.services.FileService;
import com.udacity.jwdnd.course1.cloudstorage.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.IOException;

@Controller
public class FileController {

private final FileService fileService;
private final UserService userService;

@Autowired
public FileController(FileService fileService, UserService userService) {
this.fileService = fileService;
this.userService = userService;
}


@PostMapping("/fileUpload")
public String fileUpload(@RequestParam("fileUpload") MultipartFile file, Model model) {
try {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String username = (String) authentication.getPrincipal();
User user = userService.getUserByUsername(username);
Long userId = user.getId();
fileService.saveFile(file, userId);
} catch (IllegalArgumentException e) {
model.addAttribute("errorMessage", e.getMessage());
} catch (IOException e) {
model.addAttribute("errorMessage", "Could not parse file.");
} catch (Exception e) {
model.addAttribute("errorMessage", e.getMessage());
}
return "redirect:/home";
}

@GetMapping("/files/{id}")
public ResponseEntity<byte[]> viewFile(@PathVariable("id") Integer fileId) {
// Retrieve the file from the service
File file = fileService.getFileById(fileId);

if (file == null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}

HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getFilename() + "\"");
headers.add(HttpHeaders.CONTENT_TYPE, file.getContenttype());

return new ResponseEntity<>(file.getFiledata(), headers, HttpStatus.OK);
}
@GetMapping("/files/delete/{id}")
public String deleteFile(@PathVariable("id") Integer fileId, RedirectAttributes redirectAttributes) {
try {
fileService.deleteFileById(fileId);
redirectAttributes.addFlashAttribute("successMessage", "File deleted successfully.");
} catch (Exception e) {
redirectAttributes.addFlashAttribute("errorMessage", "An error occurred while deleting the file: " + e.getMessage());
}

return "redirect:/home";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.udacity.jwdnd.course1.cloudstorage.controllers;

import com.udacity.jwdnd.course1.cloudstorage.models.File;
import com.udacity.jwdnd.course1.cloudstorage.models.User;
import com.udacity.jwdnd.course1.cloudstorage.services.FileService;
import com.udacity.jwdnd.course1.cloudstorage.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

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

@Controller
public class HomeController {

private final FileService fileService;
private final UserService userService;

@Autowired
public HomeController(FileService fileService, UserService userService) {
this.fileService = fileService;
this.userService = userService;
}

@GetMapping("/home")
public String home(Model model) {

Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String username = (String) authentication.getPrincipal();
User user = userService.getUserByUsername(username);
Long userId = user.getId();

List<File> files = null;
try {
files = fileService.getAllFiles(userId);
} catch (IOException e) {
model.addAttribute("errorMessage", "Could not parse file.");
}

model.addAttribute("files", files);

return "home";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.udacity.jwdnd.course1.cloudstorage.controllers;

import com.udacity.jwdnd.course1.cloudstorage.models.Note;
import com.udacity.jwdnd.course1.cloudstorage.services.NoteService;
import com.udacity.jwdnd.course1.cloudstorage.services.UserService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.logging.Logger;

@Controller
@RequestMapping("/notes")
public class NoteController {

private final NoteService noteService;
private final UserService userService;

@Autowired
public NoteController(NoteService noteService, UserService userService) {
this.noteService = noteService;
this.userService = userService;
}

@PostMapping("/add")
public String addNote(@ModelAttribute Note note, Authentication authentication, Model model) {
String username = authentication.getName();
Long userId = userService.getUserByUsername(username).getId();
note.setUserid(userId.intValue());

try {
noteService.addNote(note);
model.addAttribute("successMessage", "Note successfully created!");
} catch (Exception e) {
model.addAttribute("errorMessage", "There was an error creating the note. Please try again.");
}
return "redirect:/home";
}

@PostMapping("/update")
public String updateNote(@ModelAttribute Note note, Authentication authentication, Model model) {
String username = authentication.getName();
try {
noteService.updateNote(note);
model.addAttribute("successMessage", "Note successfully updated!");
} catch (Exception e) {
model.addAttribute("errorMessage", "There was an error updating the note. Please try again.");
}
return "redirect:/home";
}

@GetMapping("/delete/{noteId}")
public String deleteNote(@PathVariable Integer noteId, Model model) {
try {
noteService.deleteNote(noteId);
model.addAttribute("successMessage", "Note successfully deleted!");
} catch (Exception e) {
model.addAttribute("errorMessage", "There was an error deleting the note. Please try again.");
}
return "redirect:/home";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.udacity.jwdnd.course1.cloudstorage.controllers;

import com.udacity.jwdnd.course1.cloudstorage.mappers.UserMapper;
import com.udacity.jwdnd.course1.cloudstorage.models.User;
import com.udacity.jwdnd.course1.cloudstorage.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class UserController {

private final UserService userService;

@Autowired
public UserController(UserService userService) {
this.userService = userService;
}

@GetMapping("/login")
public String loginGet() {
return "login";
}

@GetMapping("/signup")
public String signup() {
return "signup";
}

@PostMapping("/signup")
public String signupPost(User user, RedirectAttributes redirectAttributes, Model model) {
try {
userService.addUser(user);
redirectAttributes.addFlashAttribute("successMessage", "You successfully signed up!");
return "redirect:/login";
} catch (IllegalArgumentException e) {
model.addAttribute("errorMessage", e.getMessage());
return "signup";
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.udacity.jwdnd.course1.cloudstorage.mappers;

import com.udacity.jwdnd.course1.cloudstorage.models.File;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface FileMapper {

@Insert("INSERT INTO files (filename, contenttype, filesize, userid, filedata) " +
"VALUES (#{filename}, #{contenttype}, #{filesize}, #{userid}, #{filedata})")
@Options(useGeneratedKeys = true, keyProperty = "fileId")
void insertFile(File file);

@Select("SELECT * FROM files WHERE fileId = #{fileId}")
File getFileById(Integer fileId);

@Select("SELECT * FROM files WHERE userid = #{userId}")
List<File> getFilesByUserId(Integer userId);

@Select("SELECT * FROM files WHERE userid = #{userId} AND filename = #{filename}")
File getFileByUserIdAndFileName(Integer userId, String filename);

@Delete("DELETE FROM files WHERE fileId = #{fileId}")
void deleteFileById(Integer fileId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.udacity.jwdnd.course1.cloudstorage.mappers;

import com.udacity.jwdnd.course1.cloudstorage.models.Note;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface NoteMapper {

@Insert("INSERT INTO NOTES (notetitle, notedescription, userid) VALUES (#{notetitle}, #{notedescription}, #{userid})")
@Options(useGeneratedKeys = true, keyProperty = "noteId")
int insertNote(Note note);

@Select("SELECT * FROM notes WHERE noteid = #{noteId}")
Note getNoteById(Integer noteId);

@Select("SELECT * FROM notes WHERE userid = #{userid} AND notetitle = #{notetitle}")
Note getNoteByUserIdAndTitle(Integer userid, String notetitle); // Updated method name for clarity

@Delete("DELETE FROM NOTES WHERE noteid = #{noteId}")
int deleteNote(Integer noteId);

@Update("UPDATE NOTES SET notetitle = #{notetitle}, notedescription = #{notedescription} WHERE noteid = #{noteId}")
int updateNote(Note note);
}
Loading