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

update bai 3+4+5 #52

Open
wants to merge 2 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
File renamed without changes.
4 changes: 4 additions & 0 deletions 18020020_Chu_Van_Hung/my_system/my_system.iml
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
<<<<<<< HEAD
<?xml version="1.0" encoding="UTF-8"?>
=======
<?xml version="1.0" encoding="UTF-8"?>
>>>>>>> 7834ff41e7663548ff48a128c22048c0cb26cf7a
<module type="JAVA_MODULE" version="4" />
134 changes: 81 additions & 53 deletions 18020020_Chu_Van_Hung/my_system/pom.xml
Original file line number Diff line number Diff line change
@@ -1,54 +1,82 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.uetcodecamp.jwb01</groupId>
<artifactId>my_system</artifactId>
<version>v01</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>

<dependencies>

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

<!-- JSTL for JSP -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>

<!-- Need this to compile JSP -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>

</dependencies>

<properties>
<java.version>1.8</java.version>
</properties>


<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>



<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.uetcodecamp.jwb01</groupId>
<artifactId>my_system</artifactId>
<version>v01</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>

<dependencies>
<<<<<<< HEAD
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
=======

>>>>>>> 7834ff41e7663548ff48a128c22048c0cb26cf7a
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<<<<<<< HEAD
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
=======

>>>>>>> 7834ff41e7663548ff48a128c22048c0cb26cf7a
<!-- JSTL for JSP -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>

<!-- Need this to compile JSP -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<<<<<<< HEAD
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.8.Final</version>
</dependency>
=======

>>>>>>> 7834ff41e7663548ff48a128c22048c0cb26cf7a
</dependencies>

<properties>
<java.version>1.8</java.version>
</properties>


<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>



</project>
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
<<<<<<< HEAD
package com.uetcodecamp.jwb01;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

=======
package com.uetcodecamp.jwb01;

import org.springframework.boot.SpringApplication;
Expand All @@ -17,4 +38,5 @@ public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

>>>>>>> 7834ff41e7663548ff48a128c22048c0cb26cf7a
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
package com.uetcodecamp.jwb01.controller;

import com.uetcodecamp.jwb01.model.User;
import com.uetcodecamp.jwb01.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;


@Controller
public class HelloController {

private List<User> items = new ArrayList<>();
private User session;
@Autowired
private UserRepository userRepository;

@RequestMapping("/")
public String index(Model model) {
items = userRepository.findAll();
model.addAttribute("items", items);
model.addAttribute("session", session);
return "home";
}

@GetMapping("add")
public String indexAdd()
{
return "add";
}
@PostMapping("add")
public String submitFormAdd(@RequestParam("name") String name,
@RequestParam("email") String email,
@RequestParam("phone") String phone,
@RequestParam("balance") int balance) {
User newUser = new User(name, email, phone, "123456", balance);
userRepository.save(newUser);
return "redirect:/";
}

@GetMapping("edit/{id}")
public String indexEdit(@PathVariable("id") Long id,
Model model) {
model.addAttribute("id", id);

Optional<User> user = userRepository.findById(id);
if(!user.isPresent()) {
return "edit";
}
model.addAttribute("name", user.get().getName());
model.addAttribute("email", user.get().getEmail());
model.addAttribute("phone", user.get().getPhone());
model.addAttribute("balance", user.get().getBalance());
return "edit";
}
@PostMapping("edit/{id}")
public String submitEdit(@PathVariable("id") Long id,
@RequestParam("email") String email,
@RequestParam("name") String name,
@RequestParam("phone") String phone,
@RequestParam("balance") int balance) {
Optional<User> user = userRepository.findById(id);
if(!user.isPresent()) return "redirect:/";
user.get().editUser(name, email, phone, user.get().getPassword(), balance);
userRepository.save(user.get());
return "redirect:/";
}
@PostMapping("delete")
public String indexDelete(@RequestParam("id") Long id,
Model model){
Optional<User> user = userRepository.findById(id);
if(user.isPresent()) userRepository.deleteById(id);
items = userRepository.findAll();
model.addAttribute("items", items);
return "list";
}
@RequestMapping("list")
public String indexList(Model model)
{
items = userRepository.findAll();
model.addAttribute("items", items);
return "list";
}
@PostMapping("search")
public String indexSearch(@RequestParam("name") String name,
Model model){
List<Integer> lstSearch = new ArrayList<>();
items = userRepository.findAll();
for(int i = 0; i < items.size(); i++)
{
if(items.get(i).getName().equals(name) || items.get(i).getEmail().equals(name) || items.get(i).getPhone().equals(name)) lstSearch.add(i);
}
model.addAttribute("items", items);
model.addAttribute("lstSearch", lstSearch);
return "search";
}

@GetMapping("login")
public String indexLogin()
{
if(session != null) return "redirect:/profile";
return "login";
}
@PostMapping("login")
public String submitLogin(@RequestParam("email") String email,
@RequestParam("password") String password,
Model resLogin) {
List<User> user = userRepository.findByEmail(email);
for(int i = 0; i < user.size(); i++)
if(user.get(i).isLogin(email, password)) {
session = user.get(i);
return "redirect:/profile";
}

resLogin.addAttribute("messLogin", "Đăng nhập không thành công!");
return "login";
}

@RequestMapping("logout")
public String indexLogout()
{
session = null;
return "redirect:/";
}

@RequestMapping("profile")
public String indexProfile(Model model)
{
model.addAttribute("session", session);
return "profile";
}

@GetMapping("transfer")
public String indexTranfer()
{
return "transfer";
}

@PostMapping("transfer")
public String submitTranfer(@RequestParam("id") Long id,
@RequestParam("balance") int balance,
Model resTransfer) {
Optional<User> user = userRepository.findById(id);
int myBalance = session.getBalance();
if(user.isPresent() && user.get().getId() == session.getId()) return "redirect:/profile";
if(user.isPresent() && myBalance - balance >= 0 && balance >= 0){
session.setBalance(myBalance - balance);
userRepository.save(session);
user.get().setBalance(user.get().getBalance() + balance);
userRepository.save(user.get());
return "redirect:/profile";
}
else if(myBalance - balance < 0){
resTransfer.addAttribute("messTransfer", "LỖI: Số dư trong tài khoản không đủ!");
return "transfer";
}
else if(balance < 0){
resTransfer.addAttribute("messTransfer", "LỖI: Số tiền chuyển không hợp lệ!");
return "transfer";
}
else {
resTransfer.addAttribute("messTransfer", "LỖI: ID không tồn tại!");
return "transfer";
}
}
}
Loading