com.leapfrog.sms.controller.admin.CourseController.java Source code

Java tutorial

Introduction

Here is the source code for com.leapfrog.sms.controller.admin.CourseController.java

Source

/*
 * 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.leapfrog.sms.controller.admin;

import com.leapfrog.sms.entity.Course;
import com.leapfrog.sms.service.CourseService;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author KARNAZE
 */
@Controller
@RequestMapping(value = "admin/course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @RequestMapping(method = RequestMethod.GET)
    public String index() {
        return "admin/course/index";
    }

    @RequestMapping(value = "table", method = RequestMethod.GET)
    public ModelAndView table() {
        ModelAndView mv = new ModelAndView("admin/course/table");
        mv.addObject("courses", courseService.getAll());

        return mv;
    }

    @RequestMapping(value = "edit/{id}", method = RequestMethod.GET)
    public @ResponseBody Course edit(@PathVariable("id") int id) {
        Course course = courseService.getById(id);
        return course;
    }

    @RequestMapping(value = "save", method = RequestMethod.POST)
    public @ResponseBody String save(HttpServletRequest req) {
        Course course = new Course();
        course.setName(req.getParameter("name"));
        course.setPrice(Double.parseDouble(req.getParameter("price")));
        course.setDescription(req.getParameter("description"));
        course.setStatus(req.getParameter("status").equalsIgnoreCase("1"));
        courseService.insert(course);
        return "Success";
    }

    @RequestMapping(value = "delete/{id}", method = RequestMethod.GET)
    public @ResponseBody String delete(@PathVariable("id") int id) {
        String result = "Error";
        if (courseService.delete(id) > 0) {
            result = "Success";
        }
        return result;
    }

    @RequestMapping(value = "search", method = RequestMethod.POST)
    public ModelAndView search(HttpServletRequest req) {
        ModelAndView mv = new ModelAndView("admin/course/table");
        mv.addObject("courses", courseService.search(req.getParameter("q")));

        return mv;
    }
}