Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
S4a021 Web Backend
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Charlie Darques
S4a021 Web Backend
Commits
d37d608f
Commit
d37d608f
authored
5 months ago
by
Charlie Darques
Browse files
Options
Downloads
Patches
Plain Diff
début fonctionnalité de création de thread, problème de méthode pour poster un message
parent
a25e6725
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
WEB-INF/src/controleurs/Feed.java
+12
-9
12 additions, 9 deletions
WEB-INF/src/controleurs/Feed.java
WEB-INF/src/controleurs/NewThread.java
+87
-0
87 additions, 0 deletions
WEB-INF/src/controleurs/NewThread.java
WEB-INF/src/dao/UserDAO.java
+26
-6
26 additions, 6 deletions
WEB-INF/src/dao/UserDAO.java
with
125 additions
and
15 deletions
WEB-INF/src/controleurs/Feed.java
+
12
−
9
View file @
d37d608f
...
...
@@ -39,15 +39,16 @@ public class Feed extends HttpServlet {
out
.
println
(
"<html><body><meta charset=\"utf-8\">"
);
out
.
println
(
"<title>Welcome</title>"
);
out
.
println
(
"<h1>Welcome "
+
login
+
"</h1>"
);
out
.
println
(
"<nav
class=\"menu\"
>"
);
out
.
println
(
"<ul class=\"
options_list
\">"
);
out
.
println
(
"<li class=\"option\"><a href=\"#\">\uD83C\uDFE0 Home</a></li>"
);
out
.
println
(
"<li class=\"option\"><a href=\"#\">\uD83E\uDDF5 Followed</a></li>"
);
out
.
println
(
"<li class=\"option\"><a href=\"#\">\uD83D\uDC64 Account</a></li>"
);
out
.
println
(
"<nav>"
);
out
.
println
(
"<ul class=\"
menu
\">"
);
out
.
println
(
"<li class=\"
menu_
option\"><a href=\"#\">\uD83C\uDFE0 Home</a></li>"
);
out
.
println
(
"<li class=\"
menu_
option\"><a href=\"#\">\uD83E\uDDF5 Followed</a></li>"
);
out
.
println
(
"<li class=\"
menu_
option\"><a href=\"#\">\uD83D\uDC64 Account</a></li>"
);
out
.
println
(
"</ul></nav>"
);
out
.
println
(
"<h1>Welcome "
+
login
+
"</h1>"
);
if
(!
messages
.
isEmpty
())
{
for
(
Message
message
:
messages
)
{
MyThread
msgThread
=
null
;
...
...
@@ -59,7 +60,7 @@ public class Feed extends HttpServlet {
out
.
println
(
"<div class=\"message\">"
);
out
.
println
(
"<h3 class=\"msgThread\">"
+
msgThread
.
getThreadName
()
+
"</h3>"
);
out
.
println
(
"<p class=\"msgContent\">"
+
message
.
getContent
()
+
"</p>"
);
out
.
println
(
"<form><button class=\"react\" type=\"submit\" value=\"Like\">"
);
out
.
println
(
"<form><button class=\"react\" type=\"submit\" value=\"Like\">
Like</button>
"
);
out
.
println
(
"</div>"
);
}
}
...
...
@@ -68,6 +69,8 @@ public class Feed extends HttpServlet {
out
.
println
(
"<button><a href=\"\">Discover new threads</a></button>"
);
}
out
.
println
(
"<a href=\"http://localhost:8080/s4a021-web-backend/NewThread\">Create a new thread</a>"
);
out
.
println
(
"</body>"
);
out
.
println
(
"<footer>"
);
out
.
println
(
"Connected as "
+
login
);
...
...
This diff is collapsed.
Click to expand it.
WEB-INF/src/controleurs/NewThread.java
0 → 100644
+
87
−
0
View file @
d37d608f
package
controleurs
;
import
java.io.IOException
;
import
java.io.PrintWriter
;
import
java.sql.SQLException
;
import
java.util.List
;
import
dao.ThreadDAO
;
import
dao.UserDAO
;
import
dto.MyThread
;
import
dto.User
;
import
jakarta.servlet.annotation.WebServlet
;
import
jakarta.servlet.http.HttpServlet
;
import
jakarta.servlet.http.HttpServletRequest
;
import
jakarta.servlet.http.HttpServletResponse
;
@WebServlet
(
"/NewThread"
)
public
class
NewThread
extends
HttpServlet
{
public
void
service
(
HttpServletRequest
req
,
HttpServletResponse
res
)
throws
IOException
{
if
(
req
.
getSession
().
getAttribute
(
"user"
)
!=
null
)
{
PrintWriter
out
=
res
.
getWriter
();
ThreadDAO
threadDAO
=
new
ThreadDAO
();
UserDAO
userDAO
=
new
UserDAO
();
String
login
=
(
String
)
req
.
getSession
().
getAttribute
(
"login"
);
User
user
=
(
User
)
req
.
getSession
().
getAttribute
(
"user"
);
String
threadname
=
null
;
String
message
=
null
;
boolean
newThreadWasCreated
=
false
;
if
(
req
.
getParameter
(
"threadname"
)
!=
null
)
{
threadname
=
req
.
getParameter
(
"threadname"
);
}
if
(
req
.
getParameter
(
"first_message"
)
!=
null
)
{
message
=
req
.
getParameter
(
"first_message"
);
}
if
(
threadname
!=
null
&&
message
!=
null
)
{
try
{
threadDAO
.
createThread
(
user
,
threadname
);
List
<
MyThread
>
threadsCreated
=
userDAO
.
getThreadsCreatedByUser
(
user
);
MyThread
newThread
=
null
;
for
(
MyThread
t
:
threadsCreated
)
{
if
(
t
.
getThreadName
().
equals
(
threadname
))
{
newThread
=
t
;
newThreadWasCreated
=
true
;
}
}
// PROBLEME ICI : Cannot invoke "dto.MyThread.getId()" because "<parameter2>" is null
userDAO
.
postMessage
(
user
,
newThread
,
message
);
}
catch
(
SQLException
sqle
)
{
sqle
.
getStackTrace
();
}
}
out
.
println
(
"<html><body><meta charset=\"utf-8\">"
);
out
.
println
(
"<title>New thread</title>"
);
out
.
println
(
"<div class=\"write\">"
);
out
.
println
(
"<h3>Create a new thread</h3>"
);
out
.
println
(
"<form action=\"http://localhost:8080/s4a021-web-backend/NewThread\" method=\"post\">"
);
out
.
println
(
"<input name=\"threadname\" type=\"text\" placeholder=\"Name of new thread\">"
);
out
.
println
(
"<input name=\"first_message\" type=\"text\" placeholder=\"Post the first message of your new thread!\">"
);
out
.
println
(
"<button type=\"submit\">Create</button>"
);
out
.
println
(
"</form>"
);
if
(
newThreadWasCreated
)
{
out
.
println
(
"<p class=\"information\">The thread "
+
threadname
+
" was successfully created</p>"
);
}
out
.
println
(
"<button><a href=\"http://localhost:8080/s4a021-web-backend/Welcome\">Back to feed</a></button>"
);
out
.
println
(
"</body>"
);
out
.
println
(
"<footer>"
);
out
.
println
(
"Connected as "
+
login
);
out
.
println
(
"</footer>"
);
out
.
println
(
"</html>"
);
}
else
{
res
.
sendRedirect
(
"http://localhost:8080/s4a021-web-backend/index.html"
);
}
}
}
This diff is collapsed.
Click to expand it.
WEB-INF/src/dao/UserDAO.java
+
26
−
6
View file @
d37d608f
...
...
@@ -155,10 +155,11 @@ public class UserDAO {
}
// Poster un message dans un thread
public
void
postMessage
(
User
user
,
Thread
thread
,
String
message
)
throws
SQLException
{
public
void
postMessage
(
User
user
,
My
Thread
thread
,
String
message
)
throws
SQLException
{
PreparedStatement
ps
=
this
.
con
.
prepareStatement
(
"INSERT INTO msg (userID_msg, threadID, msg) VALUES(?, ?, ?)"
);
try
{
ps
.
setInt
(
1
,
user
.
getId
());
// PROBLEME ICI
ps
.
setInt
(
2
,
(
int
)
thread
.
getId
());
ps
.
setString
(
3
,
message
);
}
...
...
@@ -181,7 +182,7 @@ public class UserDAO {
}
// Récupérer les thread auxquels un utilisateur est abonné
public
List
<
MyThread
>
getThreadsByUser
(
User
user
)
throws
SQLException
{
public
List
<
MyThread
>
getThreads
Followed
ByUser
(
User
user
)
throws
SQLException
{
List
<
MyThread
>
threads
=
new
ArrayList
<>();
PreparedStatement
ps
=
this
.
con
.
prepareStatement
(
"SELECT threadID_follow FROM follow WHERE userID_follow = ?"
);
try
{
...
...
@@ -199,6 +200,29 @@ public class UserDAO {
return
threads
;
}
public
List
<
MyThread
>
getThreadsCreatedByUser
(
User
user
)
throws
SQLException
{
List
<
MyThread
>
threads
=
new
ArrayList
<>();
PreparedStatement
ps
=
this
.
con
.
prepareStatement
(
"""
SELECT threadID, threadname FROM thread AS t
INNER JOIN userAccount AS u
ON t.userid_thread = u.userid
WHERE u.userid = ?
"""
);
try
{
ps
.
setInt
(
1
,
user
.
getId
());
ResultSet
rs
=
ps
.
executeQuery
();
while
(
rs
.
next
()){
int
threadId
=
rs
.
getInt
(
1
);
String
threadname
=
rs
.
getString
(
2
);
threads
.
add
(
new
MyThread
(
threadId
,
threadname
));
}
}
catch
(
SQLException
sqle
)
{
sqle
.
getStackTrace
();
}
return
threads
;
}
// Récupérer les messages de chaque thread à partir du plus récent
public
List
<
Message
>
getMostRecentMessages
(
User
user
)
throws
SQLException
{
List
<
Message
>
messages
=
new
ArrayList
<>();
...
...
@@ -217,10 +241,8 @@ public class UserDAO {
try
{
System
.
out
.
println
(
"user id : "
+
user
.
getId
());
ps
.
setInt
(
1
,
user
.
getId
());
System
.
out
.
println
(
"dans le try"
);
ResultSet
rs
=
ps
.
executeQuery
();
while
(
rs
.
next
()){
System
.
out
.
println
(
"dans le while"
);
int
msgID
=
rs
.
getInt
(
1
);
int
sender
=
rs
.
getInt
(
2
);
int
threadID
=
rs
.
getInt
(
3
);
...
...
@@ -230,10 +252,8 @@ public class UserDAO {
}
}
catch
(
SQLException
sqle
)
{
System
.
out
.
println
(
"erreur"
);
sqle
.
getStackTrace
();
}
System
.
out
.
println
(
"test"
);
return
messages
;
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment