Commit 2a416142 authored by Abdelmoujib Megzari's avatar Abdelmoujib Megzari
Browse files

some bugs solved and publish working

parent 5beaa47b
......@@ -100,7 +100,7 @@ public class AssociateParagraphChoice extends HttpServlet {
HttpServletResponse response,
ParagraphDAO paragraphDAO)
throws ServletException, IOException {
//TODO DELETE FOLLOWER IF != NULL AND NOT VALIDE ELSE REFUSE
HttpSession session = request.getSession(false);
if (session == null || session.getAttribute("username") == null) {
return;//TODO
......
......@@ -36,6 +36,8 @@ public class LookupStory extends HttpServlet {
@Resource(name = "jdbc/projet-web")
DataSource ds;
//TODO get HTML CODE OUT OF THE SERVELETS
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
......@@ -58,6 +60,15 @@ public class LookupStory extends HttpServlet {
case "1":
actionGetStory(request, response, new StoryDAO(ds));
break;
case "2":
actionGetMyStories(request, response, new StoryDAO(ds));
break;
case "3":
actionGetInviterStories(request,response, new StoryDAO(ds));
break;
case "4":
actionGetAllPublicStories(request, response,new StoryDAO(ds));
default:
throw new IllegalStateException("Unexpected value: " + request.getParameter("code"));
}
......@@ -155,7 +166,8 @@ public class LookupStory extends HttpServlet {
throws ServletException, IOException {
HttpSession session = request.getSession(true);
if (histoireDAO.isPublished(Integer.parseInt(request.getParameter("story_id")))) {
boolean published = histoireDAO.isPublished(Integer.parseInt(request.getParameter("story_id")));
if (published) {
}else if(session.getAttribute("username") == null){
response.sendRedirect("index.jsp");
......@@ -182,16 +194,17 @@ public class LookupStory extends HttpServlet {
request.setAttribute("owner", owner);
request.setAttribute("action", "1");
this.getServletContext().getRequestDispatcher("/LookupHistory").include(request, response);
System.out.println(((HashMap<Integer, History>)session.getAttribute("history")).get(story.getId()).size());
//this.getServletContext().getRequestDispatcher("/LookupHistory").include(request, response);
//System.out.println(((HashMap<Integer, History>)session.getAttribute("history")).get(story.getId()).size());
request.setAttribute("history", session.getAttribute("history"));
request.setAttribute("publicStory", publicStory);
request.setAttribute("invitedList",invitedList);
request.setAttribute("published", published);
this.getServletContext().getRequestDispatcher("/story.jsp").include(request, response);
if (session.getAttribute("sotries") == null) session.setAttribute("stories", new HashSet<Integer>());
((HashSet<Integer>)session.getAttribute("stories")).add(story.getId());
//if (session.getAttribute("sotries") == null) session.setAttribute("stories", new HashSet<Integer>());
//((HashSet<Integer>)session.getAttribute("stories")).add(story.getId());
}
/**
......
......@@ -189,16 +189,18 @@ public class ManageParagraph extends HttpServlet {
return; //TODO
}
int id = Integer.parseInt(request.getParameter("paragraph_id"));
try {
if (paragraphDAO.validateParagraph(id)) {
//TODO success
} else {
//TODO failure
if (paragraphDAO.validateParagraph(id)) {
response.setContentType("text/html");
try (PrintWriter out = response.getWriter()){
out.println("1");
}
} else {
response.setContentType("text/html");
try (PrintWriter out = response.getWriter()){
out.println("-1");
}
} catch (DAOException e) {
//TODO
}
}
/**
......
......@@ -8,12 +8,14 @@ package controleur;
import dao.StoryDAO;
import java.io.IOException;
import java.io.PrintWriter;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;
import javax.websocket.Session;
/**
......@@ -22,6 +24,8 @@ import javax.websocket.Session;
*/
@WebServlet(name = "PublishStory", urlPatterns = {"/PublishStory"})
public class PublishStory extends HttpServlet {
@Resource(name = "jdbc/projet-web")
DataSource ds;
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
......@@ -34,18 +38,23 @@ public class PublishStory extends HttpServlet {
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet PublishStory</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet PublishStory at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
try{
actionPublishStory(request,response, new StoryDAO(ds));
}catch (Exception e) {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet PublishStory</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet PublishStory at " + request.getContextPath() + "</h1>");
out.println("<h1>error " + e + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
}
......@@ -94,11 +103,13 @@ public class PublishStory extends HttpServlet {
throws ServletException, IOException {
//TODO VERIFY RIGHTS
HttpSession session = request.getSession(false);
System.out.println(request.getParameter("story_id"));
int storyId =Integer.parseInt(request.getParameter("story_id"));
if (session==null || session.getAttribute("username")==null){
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
out.println(-1);
out.println("not connected");
}
return;
}
......@@ -106,16 +117,24 @@ public class PublishStory extends HttpServlet {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
out.println(-1);
out.println("not the owner of the story");
}
return;
}
histoireDAO.publishStory(storyId);
if(!histoireDAO.publishStory(storyId)) {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
out.println(-1);
out.println("no path to conclusion found");
}
}
try (PrintWriter out = response.getWriter()) {
out.println(1);
}
return;
}
private void actionUnpublishStory(HttpServletRequest request,
HttpServletResponse response,
StoryDAO histoireDAO)
......
......@@ -95,15 +95,17 @@ public class ChoixDAO extends AbstractDAO {
if (paragraph.getIsConclusion()) {
publish = true;
publishChoiceDAO(choiceId);
}else{
List<Choix> choices = new ParagraphDAO(dataSource).getChoicesParagraph(paragraph.getId());
for(Choix choix: choices){
if(publishChoice(choix.getId())){
publish = true;
}
System.out.println(choiceId+"is conclusion");
}
List<Choix> choices = new ParagraphDAO(dataSource).getChoicesParagraph(paragraph.getId());
for(Choix choix: choices){
if(publishChoice(choix.getId())){
publish = true;
publishChoiceDAO(choiceId);
System.out.println(choiceId+"is published");
}
}
return publish;
}
......
......@@ -318,7 +318,7 @@ public class StoryDAO extends AbstractDAO {
private void publishDAO(int storyId) {
try(Connection conn = getConn()){
PreparedStatement st = conn.prepareStatement(" UPDATE histoire SET publiee=1 WHERE id_paragraphe=?");
PreparedStatement st = conn.prepareStatement(" UPDATE histoire SET publiee=1 WHERE id_histoire=?");
st.setInt(1, storyId);
st.executeQuery();
} catch (SQLException exception) {
......
......@@ -2,12 +2,12 @@
<div class="choices" id="choices${paragraph.id}">
<c:forEach items="${choiceList}" var="choice">
<c:if test="${choice['idParagrapheSuite']==0}" var="new" scope="request">
<span class="choice ${choice['published']?'':'edit'}" style ="${choice['published']?'':'display:none;'}" onclick="${'newParagraphe'}(${choice['idParagraphe']},${choice['id']},${storyid})">
<span class="choice ${choice['published']?'':'edit'}" id ="choice${choice['id']}" style ="${choice['published']?'':'display:none;'}" onclick="${'newParagraphe'}(${choice['idParagraphe']},${choice['id']},${storyid})">
<c:out value="${choice['texte']}"/>
</span>
</c:if>
<c:if test="${choice['idParagrapheSuite']!=0}" var="new" scope="request">
<span class="choice ${choice['published']?'':'edit'}" style ="${choice['published']?'':'display:none;'}" onclick="${'choose'}(${choice['idParagraphe']},${choice['idParagrapheSuite']},${storyid})">
<span class="choice ${choice['published']?'':'edit'}" id ="choice${choice['id']}" style ="${choice['published']?'':'display:none;'}" onclick="${'choose'}(${choice['idParagraphe']},${choice['idParagrapheSuite']},${storyid})">
<c:out value="${choice['texte']}"/>
</span>
</c:if>
......
......@@ -147,14 +147,20 @@ function submit_edit_paragraphe(paragraph_id, save, notvalid) {
$.post("ManageParagraph", {action: act, paragraph_id: paragraph_id, text: text}).done(function (data) {
console.log("worked")
if(save) {
$(`#paragrapheText${paragraph_id}`).empty().append(text);
if(parseInt(data)===1){
$(`#paragrapheText${paragraph_id}`).empty().append(text);
document.getElementById(`modifyParagraph${paragraph_id}`).style.display = "block";
}else{
alert("couldn't validate paragrapheplease add a choice or conclusion and try again!!")
}
}else if(notvalid){
$(`#paragrapheText${paragraph_id}`).empty().append("this paragraph is not valid yet");
document.getElementById(`modifyParagraph${paragraph_id}`).style.display = "block";
}
else{
annuler_modification(paragraph_id);
document.getElementById(`modifyParagraph${paragraph_id}`).style.display = "block";
}
document.getElementById(`modifyParagraph${paragraph_id}`).style.display = "block";
}).fail(function (jqXHR, textStatus, errorThrown) {
return console.error(errorThrown)
})
......@@ -184,7 +190,8 @@ function newParagraphe(paragraphToContinueID, choice_id){
let paragraphFollowerID = createParagraph(choice_id);
console.log(paragraphFollowerID);
if (paragraphFollowerID !== -1) {
choose(paragraphToContinueID,paragraphFollowerID,-1);
document.getElementById(`choice${choice_id}`).setAttribute("onclick",`choose(${paragraphToContinueID},${paragraphFollowerID})`)
choose(paragraphToContinueID,paragraphFollowerID,story_id);
}
}
......@@ -239,14 +246,14 @@ function deleteParagraphe(id){
function associate(id){
//TODO bug detected after association the new paragraphe isn't deleted and is forever locked
let follower = parseInt(document.getElementById(`link${id}`).value);
console.log(follower)
console.log(last_lock)
console.log(follower);
console.log(last_lock);
$.post("AssociateParagraphChoice", {choice_id:last_lock , paragraphFollower: follower}).done(function (data) {
console.log("done")
console.log(data)
choose(last_lock_first,follower,0)//TODO change story id
console.log("done");
console.log(data);
choose(last_lock_first,follower,story_id);
}).fail(function (jqXHR, textStatus, errorThrown) {
return console.error(errorThrown);
});
......@@ -257,8 +264,11 @@ function associate(id){
function publish(story_id){
$.post("PublishStory", {story_id: story_id}).done(function (data) {
if(parseInt(data)===1) {
console.log(data);
alert("story published succesfuly");
document.getElementById("switch_mode").style.display="block";
}else {
console.log(data);
alert("you dont have the rignht to perform this action or an error has occured")
}
}
......@@ -266,3 +276,5 @@ function publish(story_id){
return console.error(errorThrown);
});
}
<%@ page pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Story Time</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="UTF-8"/>
<link rel="stylesheet" href="style.css?java=<?=time()?">
<link rel="stylesheet" href="style2.css?java=<?=time()?">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="index.js"></script>
</head>
<body>
<%@ include file="menu.jsp" %>
<%@ include file="sideMenu.jsp" %>
<%@ include file="login.jsp" %>
<div class="main_block">
<div class="main_title">
<h1>A game where YOU are the story!</h1>
</div>
<div class="storie_list">
<h3>my stories</h3>
<div class="my stories">
<jsp:include page="/LookupStory?code=2" />
</div>
<h3>invited stories</h3>
<div class="invited stories">
<jsp:include page="/LookupStory?code=3" />
</div>
<h3>public stories</h3>
<div class="invited stories">
<jsp:include page="/LookupStory?code=4" />
</div>
</div>
<h1 style="display:flex; justify-content: center; margin-top:70px;">Or...</h1>
<c:choose>
<c:when test="${empty sessionScope.username}">
<h2 class="sign_up_now"><span onclick="showLogin()">Login/Sign up </span> now and start creating stories!</h2>
</c:when>
<c:otherwise>
<div class="create_story">
<a href="createstory.jsp">Create a story</a>
</div>
</c:otherwise>
</c:choose>
</div>
</body>
</html>
......@@ -7,6 +7,7 @@
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="UTF-8"/>
<link rel="stylesheet" href="style.css">
<script>var story_id=${story.id};</script>
<script src="index.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
......@@ -16,14 +17,14 @@
<%@ include file="login.jsp" %>
<div style="position: absolute; top: 80px;left:0;width:100%">
<c:if test="${edit}" var="maVariable" scope="request">
<div class="edit_mode" id="switch_mode" onclick="switchMode()"><button>edit_mode</button></div>
<div class="edit_mode" id="switch_mode" style="${published?"display:block;":"display:none"}" onclick="switchMode()"><button>edit_mode</button></div>
</c:if>
<c:if test="${owner}" var="maVariable" scope="request">
<div class="edit_mode" id="switch_mode" onclick="Publish(${story.id})"><button>publish</button></div>
<div class="edit_mode" id="publish" onclick="publish(${story.id})"><button>publish</button></div>
<c:if test="${!publicStory}" var="maVariable" scope="request">
<input type="text" placeholder="login du nouveau auteur" id="user_to_invite"></form>
<div class="edit_mode" id="switch_mode" onclick="inviteAuthor(${story.id})"><button type="button">inviter auteur</button></div>
<div class="edit_mode" id="invite" onclick="inviteAuthor(${story.id})"><button type="button">inviter auteur</button></div>
<span id="invited">invited
<c:forEach items="${invitedList}" var="invited">
<div>${invited}</div>
......@@ -43,5 +44,8 @@
last=${paragraph.id};
</c:forEach>
</script>
<script>
${published?"":"window.onload=switchMode();"}
</script>
</body>
</html>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment