package com.wxpn.tutorial.servlet;

import java.io.IOException;

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

import com.wxpn.tutorial.bean.news.News;
import com.wxpn.tutorial.control.news.MasterMgr;
import com.wxpn.tutorial.control.news.NewsMgr;

/**
 * Servlet implementation class for Servlet: AddNewsServlet
 *
 */
 public class AddNewsServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
    /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public AddNewsServlet() {
		super();
	}   	
	
	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		if(request.getSession(true).getAttribute("username")==null){
			response.sendRedirect("admin/loginerror.jsp");
		}
		String subject = request.getParameter("subject");
		String content = request.getParameter("Content");
		String pic = request.getParameter("pic");
		int classid = Integer.parseInt(request.getParameter("classid"));
		News news =new News();
		news.setTopic(subject);
		news.setContent(content);
		news.setPublishuser(request.getSession(true).getAttribute("username").toString());
		news.setCategoryid(classid);
		news.setPicture(pic);
		NewsMgr newsMgr =new NewsMgr();
		int id=newsMgr.add(news);
		
		MasterMgr masterMgr=new MasterMgr();
		masterMgr.addnewsnum(request.getSession(true).getAttribute("username").toString());
		
		response.sendRedirect("admin/savenews.jsp?id="+id);
	}   	  	    
}