Updated SQLDriver to get all cards. Added card page.

This commit is contained in:
Jordan Mason
2020-09-28 12:23:09 -04:00
parent 73890402f7
commit d0f4fbf1a7
5 changed files with 177 additions and 0 deletions

View File

@ -55,6 +55,46 @@ public class SQLDriver {
return null;
}
public ArrayList<Card> getAllCards() {
ArrayList<Card> topdeck = new ArrayList<Card>();
try {
c = DriverManager.getConnection("jdbc:sqlite:cards.db");
c.setAutoCommit(false);
Statement stmt = c.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM CARDS;");
String cardName = "";
int manaCost = 0;
int power = 0;
int toughness = 0;
String description = "";
String image = "";
String creatureType = "";
while (rs.next()) {
cardName = rs.getString("CARDNAME");
manaCost = rs.getInt("MANACOST");
power = rs.getInt("POWER");
toughness = rs.getInt("TOUGHNESS");
description = rs.getString("DESCRIPTION");
image = rs.getString("IMAGE");
creatureType = rs.getString("CREATURETYPE");
Card card = new Card(cardName, manaCost, power, toughness, description, image, creatureType);
topdeck.add(card);
}
rs.close();
stmt.close();
c.close();
return topdeck;
} catch (Exception e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
System.err.println("Returning null.");
return null;
}
public boolean insertCard(Card card) {
Statement stmt = null;
try {

View File

@ -0,0 +1,34 @@
package MTGClone.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import MTGClone.Card;
import MTGClone.SQLDriver;
import java.util.ArrayList;
@Controller
public class CardController {
@GetMapping({"/card"})
public String hello(Model model) {
ArrayList<Card> allCards = (new SQLDriver()).getAllCards();
model.addAttribute("allcards", allCards);
return "card";
}
@PostMapping({"/card"})
public String greet(@RequestParam("cardname") String cardname, @RequestParam("manacost") int manacost,
@RequestParam("power") int power, @RequestParam("toughness") int toughness, @RequestParam("description") String description, @RequestParam("creaturetype") String creaturetype,
ModelMap modelMap) {
Card newCard = new Card(cardname, manacost, power, toughness, description, "", creaturetype);
SQLDriver d = new SQLDriver();
d.insertCard(newCard);
return "card";
}
}

View File

@ -0,0 +1,67 @@
<!DOCTYPE html>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html lang="en">
<head>
<style>
.container {
width: 350px;
clear: both;
}
.container input {
width: 100%;
clear: both;
}
</style>
<meta charset="UTF-8">
<title>Hello ${name}!</title>
</head>
<body>
<h2 class="hello-title">Hello ${name}!</h2>
<div class="container">
<form action="/card" method="post">
<label for="cardname">Card Name:</label>
<input type="text" id="cardname" name="cardname"><br>
<label for="manacost">Mana Cost:</label>
<input type="text" id="manacost" name="manacost"><br>
<label for="power">Power:</label>
<input type="text" id="power" name="power"><br>
<label for="toughness">Toughness:</label>
<input type="text" id="toughness" name="toughness"><br>
<label for="description">Description:</label>
<input type="text" id="description" name="description"><br>
<label for="creaturetype">Creature Type:</label>
<input type="text" id="creaturetype" name="creaturetype"><br>
<br> <input style="width:30%;display:block;margin:0 auto;" type="submit" value="Submit">
</form>
</div>
<table style="width:100%">
<tr>
<th>Card Name</th>
<th>Mana Cost</th>
<th>Power</th>
<th>Toughness</th>
<th>Description</th>
<th>Creature Type</th>
</tr>
<tr>
<c:if test="${not empty allcards}">
<c:forEach var="listValue" items="${allcards}">
<td>${listValue.cardName}</td>
<td>${listValue.manaCost}</td>
<td>${listValue.power}</td>
<td>${listValue.toughness}</td>
<td>${listValue.description}</td>
<td>${listValue.creatureType}</td>
</c:forEach>
</c:if>
</tr>
</table>
</body>
</html>