Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.musiccorp.controller; import com.musiccorp.model.Artist; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.logging.Logger; import org.apache.log4j.spi.LoggerFactory; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; /** * * @author Jonathan */ @Controller public class MusicCorpController { // TODO create DAO + Service layer static Logger log = Logger.getLogger(MusicCorpController.class.getName()); Map<Integer, Artist> database = new HashMap<Integer, Artist>(); /*== Populate dummy data until DAO Layer implemented ==*/ @RequestMapping(value = "/artist/dummy", method = RequestMethod.GET) public @ResponseBody Artist getDummyArtist() { Artist a = new Artist(); a.setId(9999); a.setName("Bill Callahan"); a.setProfile("Musician"); ArrayList genre = new ArrayList<String>(); genre.add("GoodStuff"); a.setGenre(new ArrayList<String>(genre)); return a; } /*== return list of all Artists in database ==*/ @RequestMapping(value = "/artist/", method = RequestMethod.GET) public @ResponseBody Map<Integer, Artist> getAllArtist() { log.info("== Listed ALL Aritsts"); // todo service().dao.fetchAll() return database; } /* == Post new Artist entry == */ @RequestMapping(value = "/artist/", method = RequestMethod.POST) public @ResponseBody Artist createArtist(@RequestBody Artist artist) { database.put(artist.getId(), artist); return artist; } /* == return all Artists by Genre ==*/ @RequestMapping(value = "/artist/genre/{genre}", method = RequestMethod.GET) public @ResponseBody ArrayList<Artist> getArtistByGenre(@PathVariable("genre") String genre) { ArrayList<Artist> artistList = new ArrayList<Artist>(); ArrayList<String> genreList = new ArrayList<String>(); Artist artist = new Artist(); log.info("======= Searching for genre " + genre); log.info("======= Searching against database size " + database.size()); for (int i = 1; i <= database.size(); i++) { log.info("get artist at index i (" + i + ")"); artist = database.get(i); log.info("\n" + artist.toString() + "\n"); genreList = artist.getGenre(); for (String genreCat : genreList) { if (genre.equals(genreCat)) { artistList.add(artist); } } } return artistList; } /* == Update Artist data ==*/ @RequestMapping(value = "/artist/{id}", method = RequestMethod.PUT) public @ResponseBody Artist updateArtist(@PathVariable("id") int id) { Artist artistUpdated = database.get(id); // update artist name artistUpdated.setName("Billy Corrigan"); return artistUpdated; } /* == Delete an Artists data == */ @RequestMapping(value = "/artist/{id}", method = RequestMethod.DELETE) public ResponseEntity<?> deleteArtist(@PathVariable("id") int artistID) { Artist artist = database.get(artistID); database.remove(artist); log.info("== Artist data DELETED"); database.remove(artist.getId()); return new ResponseEntity<String>(HttpStatus.NO_CONTENT); } }