-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdminController.java
More file actions
86 lines (71 loc) · 3.08 KB
/
AdminController.java
File metadata and controls
86 lines (71 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.example.init.controllers;
import java.security.Principal;
import java.util.List;
import javax.transaction.Transactional;
import com.example.init.models.Coders;
import com.example.init.repositories.CodersRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
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.servlet.view.RedirectView;
@Controller
public class AdminController {
@Autowired
CodersRepository codersRepository;
@Autowired
BCryptPasswordEncoder bCryptPasswordEncoder;
@GetMapping("/admin")
String admin(Model model) {
List<Coders> coders = codersRepository.findAll();
model.addAttribute("coders", coders);
return "admin";
}
@PostMapping("/admin")
RedirectView addCoder(String email, String username, String password, String firstName, String lastName,
String dateOfBirth, String bio) {
Coders newCoder = new Coders(email, username, bCryptPasswordEncoder.encode(password), firstName, lastName,
dateOfBirth, bio);
codersRepository.save(newCoder);
return new RedirectView("/admin");
}
@GetMapping("/coder")
public String getCoder(@RequestParam long id, Model model, Principal principal) {
Coders coder = codersRepository.findById(id).get();
model.addAttribute("coder", coder);
return "coder";
}
@Transactional
@GetMapping("/del/{id}")
public RedirectView delete(@PathVariable String id, Principal principal, Model model) {
Coders coder = codersRepository.findById(Long.parseLong(id)).orElseThrow();
model.addAttribute("coder", principal.getName());
codersRepository.delete(coder);
return new RedirectView("/admin");
}
@GetMapping("/form/{id}")
public String showUpdateForm(@PathVariable("id") long id, Model model) {
Coders coder = codersRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("Invalid user Id:" + id));
model.addAttribute("coder", coder);
return "putForm";
}
@Transactional
@PostMapping("/upd/{id}")
public RedirectView updateUser(@PathVariable("id") long id, Coders coder) {
Coders updatedCoder = codersRepository.findById(id).orElseThrow();
updatedCoder.setEmail(coder.getEmail());
updatedCoder.setUsername(coder.getUsername());
updatedCoder.setPassword(coder.getPassword());
updatedCoder.setFirstName(coder.getFirstName());
updatedCoder.setLastName(coder.getLastName());
updatedCoder.setDateOfBirth(coder.getDateOfBirth());
updatedCoder.setBio(coder.getBio());
codersRepository.save(updatedCoder);
return new RedirectView("/admin");
}
}