Skip to content
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
13 changes: 13 additions & 0 deletions demo/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

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

}
16 changes: 16 additions & 0 deletions demo/DemoApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

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

}


*/
21 changes: 21 additions & 0 deletions demo/GreetingController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example.demo;

/*import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class GreetingController {

@GetMapping("/")
public String greeting(Model model) {

model.addAttribute("name", "Mundo");

return "index";
}

}

*/

48 changes: 48 additions & 0 deletions demo/controller/GameResController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.example.demo.controller;

import java.security.Principal;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.model.Game;
import com.example.demo.model.User;
import com.example.demo.service.GameService;
import com.example.demo.repository.UserRepository;

@RestController
@RequestMapping("/api/profile")
public class GameResController {

@Autowired
private GameService purchaseService;
@Autowired
private UserRepository userService;

@GetMapping("/games")
public ResponseEntity<Page<Game>> showMore(Model model, HttpServletRequest request, HttpServletResponse response) {


int pageRequested = 0;
String numPage = request.getParameter("numPage");
if (numPage != null) {
pageRequested = Integer.parseInt(numPage);
}

Page<Game> page = purchaseService.findAll( pageRequested);
return ResponseEntity.ok(page);

}

}
227 changes: 227 additions & 0 deletions demo/controller/GameWebController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
package com.example.demo.controller;

import java.io.IOException;
import java.security.Principal;
import java.sql.SQLException;
import java.util.List;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;

import com.example.demo.model.Game;
import org.hibernate.engine.jdbc.BlobProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
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.multipart.MultipartFile;

import com.example.demo.service.GameService;
//import com.example.demo.service.ShopService;

@Controller
public class GameWebController {

@Autowired
private GameService gamesService;

//@Autowired
//private ShopService shopService;

@ModelAttribute
public void addAttributes(Model model, HttpServletRequest request) {

Principal principal = request.getUserPrincipal();

if (principal != null) {

model.addAttribute("logged", true);
model.addAttribute("userName", principal.getName());
model.addAttribute("admin", request.isUserInRole("ADMIN"));

} else {
model.addAttribute("logged", false);
}
}

@GetMapping("/games")
public String showGames(Model model) {

model.addAttribute("games", gamesService.findAll(0));
model.addAttribute("currentPage", 0);
return "games";
}










@GetMapping("/index")
public String Index(Model model) {

//model.addAttribute("ga", gamesService.findAll());

return "index";
}
@GetMapping("/Usuario/{id}")
public String User(Model model) {

//model.addAttribute("ga", gamesService.findAll());

return "Usuario";
}

@GetMapping("/games/{id}")
public String showGame(Model model, @PathVariable long id) {

Optional<Game> game = gamesService.findById(id);
if (game.isPresent()) {
model.addAttribute("game", game.get());
return "game";
} else {
return "games";
}

}

@GetMapping("/games/{id}/image")
public ResponseEntity<Object> downloadImage(@PathVariable long id) throws SQLException {

Optional<Game> game = gamesService.findById(id);
if (game.isPresent() && game.get().getImageFile() != null) {

Resource file = new InputStreamResource(game.get().getImageFile().getBinaryStream());

return ResponseEntity.ok().header(HttpHeaders.CONTENT_TYPE, "image/jpeg")
.contentLength(game.get().getImageFile().length()).body(file);

} else {
return ResponseEntity.notFound().build();
}
}
@GetMapping("/games/{id}/Checkout")
public String Checkout(Model model, @PathVariable long id) {

Optional<Game> game = gamesService.findById(id);
if (game.isPresent()) {
model.addAttribute("game", game.get());
return "checkout";
} else {
return "game";
}
}



/*@GetMapping("/removebook/{id}")
public String removeBook(Model model, @PathVariable long id) {

Optional<Games> book = gamesService.findById(id);
if (book.isPresent()) {
gamesService.delete(id);
model.addAttribute("book", book.get());
}
return "removedbook";
}

*/

@GetMapping("/Addgames")
public String newgame(Model model) {

//model.addAttribute("availableShops", shopService.findAll());

return "Addgames";
}


/*
@PostMapping("/Addgames")
public String newGameProcess(Model model, Game game, MultipartFile imageField, @RequestParam List<Long> selectedShops) throws IOException {

if (!imageField.isEmpty()) {
game.setImageFile(BlobProxy.generateProxy(imageField.getInputStream(), imageField.getSize()));
game.setImage(true);
}

//games.setShops(shopService.findById(selectedShops));

gamesService.save(game);

model.addAttribute("gameId", game.getId());

return "redirect:/games/"+ game.getId();
}

*/

/*@GetMapping("/game.html/{id}")
public String showGames(Model model, @PathVariable long id) {

Optional<Games> games = gamesService.findById(id);
if (games.isPresent()) {
model.addAttribute("games", games.get());
return "single";
} else {
return "Products";
}
}*/


/*

@PostMapping("/editgames")
public String editBookProcess(Model model, Game game, boolean removeImage, MultipartFile imageField)
throws IOException, SQLException {

updateImage(game, removeImage, imageField);

gamesService.save(game);

model.addAttribute("gameId", game.getId());

return "redirect:/single/"+ game.getId();
}

*/






private void updateImage(Game game, boolean removeImage, MultipartFile imageField) throws IOException, SQLException {

if (!imageField.isEmpty()) {
game.setImageFile(BlobProxy.generateProxy(imageField.getInputStream(), imageField.getSize()));
game.setImage(true);
} else {
if (removeImage) {
game.setImageFile(null);
game.setImage(false);
} else {
// Maintain the same image loading it before updating the book
Game dbGame = gamesService.findById(game.getId()).orElseThrow();
if (dbGame.getImage()) {
game.setImageFile(BlobProxy.generateProxy(dbGame.getImageFile().getBinaryStream(),
dbGame.getImageFile().length()));
game.setImage(true);
}
}
}
}

}
18 changes: 18 additions & 0 deletions demo/controller/LoginWebController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class LoginWebController {

@RequestMapping("/login")
public String login() {
return "login";
}

@RequestMapping("/loginerror")
public String loginerror() {
return "loginerror";
}
}
Loading