forked from astrocat879/wizard-cats
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
216 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
backend/src/main/java/WizardCats/backend/controller/UserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package WizardCats.backend.controller; | ||
|
||
import WizardCats.backend.entities.UserEntity; | ||
import WizardCats.backend.service.UserService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequestMapping("/api/users") | ||
public class UserController { | ||
|
||
private final UserService userService; | ||
|
||
@Autowired | ||
public UserController(UserService userService) { | ||
this.userService = userService; | ||
} | ||
|
||
@GetMapping("/") | ||
public String root() { | ||
return "Welcome."; | ||
} | ||
|
||
@GetMapping("/hello") | ||
public String hello(@RequestParam String request) { | ||
return "Hello! " + request; | ||
} | ||
|
||
@PostMapping("/register") | ||
public UserEntity registerUser(@RequestBody AccountRequest request) { | ||
return userService.registerUser(request.getUsername(), request.getPassword()); | ||
} | ||
|
||
@GetMapping("/signIn") | ||
public UserEntity signIn(@RequestParam AccountRequest request) { | ||
return userService.signIn(request.getUsername(), request.getPassword()); | ||
} | ||
|
||
@PostMapping("/updateStats") | ||
public UserEntity updateStats(@RequestBody AccountRequest request, | ||
@RequestParam Integer playedAdd, | ||
@RequestParam Integer winsAdd) { | ||
return userService.updateStats(request.getUsername(), playedAdd, winsAdd); | ||
} | ||
|
||
public static class AccountRequest { | ||
private String username; | ||
private String password; | ||
|
||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
public void setUsername(String username) { | ||
this.username = username; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
public void setPassword(String password) { | ||
this.password = password; | ||
} | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
backend/src/main/java/WizardCats/backend/entities/UserEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package WizardCats.backend.entities; | ||
|
||
import javax.persistence.*; | ||
|
||
@Table(name = "users") | ||
@Entity | ||
public class UserEntity { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@Column(nullable = false) | ||
private String username; | ||
|
||
@Column(nullable = false) | ||
private String password; | ||
|
||
@Column(nullable = false) | ||
private int gamesPlayed; | ||
|
||
@Column(nullable = false) | ||
private int wins; | ||
|
||
public UserEntity() { | ||
this.username = ""; | ||
this.password = ""; | ||
this.gamesPlayed = 0; | ||
this.wins = 0; | ||
} | ||
|
||
public UserEntity(String username, String password) { | ||
this.username = username; | ||
this.password = password; | ||
this.gamesPlayed = 0; | ||
this.wins = 0; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
public void setUsername(String username) { | ||
this.username = username; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
public void setPassword(String password) { | ||
this.password = password; | ||
} | ||
|
||
public int getGamesPlayed() { | ||
return gamesPlayed; | ||
} | ||
|
||
public void setGamesPlayed(int gamesPlayed) { | ||
this.gamesPlayed = gamesPlayed; | ||
} | ||
|
||
public int getWins() { | ||
return wins; | ||
} | ||
|
||
public void setWins(int wins) { | ||
this.wins = wins; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
backend/src/main/java/WizardCats/backend/repository/UserRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package WizardCats.backend.repository; | ||
|
||
import WizardCats.backend.entities.UserEntity; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface UserRepository extends JpaRepository<UserEntity, Long> { | ||
UserEntity findByUsername(String username); | ||
} |
45 changes: 45 additions & 0 deletions
45
backend/src/main/java/WizardCats/backend/service/UserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package WizardCats.backend.service; | ||
|
||
import WizardCats.backend.entities.UserEntity; | ||
import WizardCats.backend.repository.UserRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class UserService { | ||
private final UserRepository userRepository; | ||
|
||
@Autowired | ||
public UserService(UserRepository userRepository) { | ||
this.userRepository = userRepository; | ||
} | ||
|
||
public UserEntity registerUser(String username, String password) { | ||
UserEntity newUser = new UserEntity(username, password); | ||
return userRepository.save(newUser); | ||
} | ||
|
||
public UserEntity signIn(String username, String password) { | ||
UserEntity user = userRepository.findByUsername(username); | ||
if (user == null) { | ||
return null; | ||
} | ||
if (password.equals(user.getPassword())) { | ||
return user; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
public UserEntity updateStats(String username, int playedAdd, int winsAdd) { | ||
UserEntity user = userRepository.findByUsername(username); | ||
|
||
if (user != null) { | ||
user.setGamesPlayed(user.getGamesPlayed() + playedAdd); | ||
user.setWins(user.getWins() + winsAdd); | ||
return userRepository.save(user); | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,9 @@ | |
@SpringBootTest | ||
class BackendApplicationTests { | ||
|
||
/* | ||
@Test | ||
void contextLoads() { | ||
} | ||
|
||
*/ | ||
} |