Skip to content

Commit

Permalink
add user CRUD components
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoella committed Aug 29, 2023
1 parent 6939940 commit 076b950
Show file tree
Hide file tree
Showing 10 changed files with 216 additions and 5 deletions.
3 changes: 2 additions & 1 deletion backend/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
id 'java'
id 'org.springframework.boot' version '3.1.3'
id 'org.springframework.boot' version '2.7.2'
id 'io.spring.dependency-management' version '1.1.3'
}

Expand All @@ -20,6 +20,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'mysql:mysql-connector-java:8.0.27'
implementation 'javax.persistence:javax.persistence-api:2.2'
}

tasks.named('test') {
Expand Down
Empty file modified backend/run.sh
100644 → 100755
Empty file.
8 changes: 8 additions & 0 deletions backend/setup_mysql.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,13 @@ CREATE DATABASE wizard_cats;
CREATE USER 'Admin'@'localhost' IDENTIFIED BY 'Password';
GRANT ALL PRIVILEGES ON wizard_cats.* TO 'Admin'@'localhost';
FLUSH PRIVILEGES;
USE wizard_cats;
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
password VARCHAR(100) NOT NULL,
games_played INT DEFAULT 0,
wins INT DEFAULT 0
);
EXIT;
MYSQL_SCRIPT
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@EntityScan(basePackages = "WizardCats.backend.*")
@EnableJpaRepositories
@ComponentScan(basePackages = { "WizardCats.backend.base.*", "WizardCats.backend.controller", "WizardCats.backend.service" })
public class BackendApplication {

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

}
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 backend/src/main/java/WizardCats/backend/entities/UserEntity.java
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;
}
}
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 backend/src/main/java/WizardCats/backend/service/UserService.java
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;
}
}
1 change: 0 additions & 1 deletion backend/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

spring.datasource.url=jdbc:mysql://localhost:3306/wizard_cats
spring.datasource.username=Admin
spring.datasource.password=Password
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
@SpringBootTest
class BackendApplicationTests {

/*
@Test
void contextLoads() {
}

*/
}

0 comments on commit 076b950

Please sign in to comment.