package org.launchcode.app.controllers;
import org.launchcode.app.data.Artifact; import org.launchcode.app.data.ArtifactRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.Errors; import org.springframework.web.bind.annotation.*;
import javax.persistence.Id; import javax.validation.Valid; import java.util.HashMap;
@Controller public class HomeController {
@Autowired private ArtifactRepository artifactRepository; @RequestMapping(value = "/") public String index(Model model) {
// model.addAttribute("database", "Database"); // model.addAttribute("images", "Images"); model.addAttribute("add", "Add"); model.addAttribute("search", "Search"); model.addAttribute("edit", "Edit"); model.addAttribute("delete", "Delete"); // model.addAttribute("thePost", thePost); model.addAttribute("artifacts", artifactRepository.findAll()); HashMap actionChoices = new HashMap<>(); return "index"; }
@GetMapping("add") public String displayAddArtifactForm(Model model) { model.addAttribute(new Artifact()); model.addAttribute("artifact", new Artifact()); return "add"; } @PostMapping("add") @ResponseBody public String processAddArtifactForm(@ModelAttribute @Valid Artifact newArtifact, Errors errors) { if (errors.hasErrors()) { return "add"; } else { artifactRepository.save(newArtifact); return "index"; } } @RequestMapping(value="edit/{postId}", method = RequestMethod.GET) public String displayEditForm(Model model, @PathVariable int postId, Error error){ Artifact thePost = artifactRepository.findById(postId).get(); artifactRepository.save(thePost); model.addAttribute("thePost", thePost); return "edit"; } @RequestMapping(value="edit/{post_id}", method=RequestMethod.POST) public String processEditForm(@RequestParam(value="edit/{post_id}") @PathVariable int post_id, @RequestParam(value="name") String name, @RequestParam(value="location") String location, @RequestParam(value="description") String description, Model model) { Artifact thePost = artifactRepository.findById(post_id).get(); thePost.setName(name); thePost.setLocation(location); thePost.setDescription(description); artifactRepository.save(thePost); return "index"; } @RequestMapping(value = "delete/{id}", method = RequestMethod.GET) public String delete(@PathVariable("id") int id) { artifactRepository.deleteById(id); return "index"; } @GetMapping("view/{artifactId}") public String displayViewArtifactId(Model model, @PathVariable int Id) { return "index"; }
// @GetMapping("view/{Images}") // public String displayViewImagesId(Model model, @PathVariable int Id) { // return "images"; // } }
Ok moving on This is my entire home controller.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
package org.launchcode.app.controllers;
import org.launchcode.app.data.Artifact;
import org.launchcode.app.data.ArtifactRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;
import javax.persistence.Id;
import javax.validation.Valid;
import java.util.HashMap;
@Controller
public class HomeController {
// model.addAttribute("database", "Database");
// model.addAttribute("images", "Images");
model.addAttribute("add", "Add");
model.addAttribute("search", "Search");
model.addAttribute("edit", "Edit");
model.addAttribute("delete", "Delete");
// model.addAttribute("thePost", thePost);
model.addAttribute("artifacts", artifactRepository.findAll());
HashMap actionChoices = new HashMap<>();
return "index";
}
// @GetMapping("view/{Images}")
// public String displayViewImagesId(Model model, @PathVariable int Id) {
// return "images";
// }
}
Ok moving on
This is my entire home controller.