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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Charlie Darques
S4a021 Web Backend
Commits
27d83f6d
Commit
27d83f6d
authored
1 month ago
by
Charlie Darques
Browse files
Options
Downloads
Patches
Plain Diff
possibilité de suivre un thread depuis la page discover
parent
cfd380b4
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
WEB-INF/src/controleurs/Discover.java
+18
-1
18 additions, 1 deletion
WEB-INF/src/controleurs/Discover.java
WEB-INF/src/controleurs/FollowThread.java
+39
-2
39 additions, 2 deletions
WEB-INF/src/controleurs/FollowThread.java
WEB-INF/src/dao/UserDAO.java
+13
-0
13 additions, 0 deletions
WEB-INF/src/dao/UserDAO.java
with
70 additions
and
3 deletions
WEB-INF/src/controleurs/Discover.java
+
18
−
1
View file @
27d83f6d
...
...
@@ -39,6 +39,16 @@ public class Discover extends HttpServlet {
out
.
println
(
PageGeneration
.
generateNavMenu
());
List
<
MyThread
>
followedThreads
=
null
;
try
{
followedThreads
=
userDao
.
getThreadsFollowedByUser
(
user
);
}
catch
(
SQLException
sqle
)
{
sqle
.
getStackTrace
();
}
if
(!
messages
.
isEmpty
())
{
for
(
Message
message
:
messages
)
{
MyThread
msgThread
=
null
;
...
...
@@ -49,6 +59,7 @@ public class Discover extends HttpServlet {
}
User
sender
=
userDao
.
getUserById
(
message
.
getSenderId
());
String
senderName
=
sender
.
getUserName
();
boolean
followed
=
followedThreads
.
contains
(
msgThread
);
out
.
println
(
"<div class=\"message\">"
);
out
.
println
(
"<h3 class=\"msgThread\">"
+
msgThread
.
getThreadName
()
+
"</h3>"
);
...
...
@@ -57,7 +68,13 @@ public class Discover extends HttpServlet {
out
.
println
(
"<form class=\"followThread\" action=\"http://localhost:8080/s4a021-web-backend/FollowThread\" method=\"post\">"
);
out
.
println
(
"<input name=\"threadid\" type=\"hidden\" value=\""
+
msgThread
.
getId
()
+
"\">"
);
out
.
println
(
"<button type=\"submit\">Follow</button>"
);
if
(
followed
)
{
out
.
println
(
"<button type=\"submit\">Followed</button>"
);
}
else
{
out
.
println
(
"<button type=\"submit\">Follow</button>"
);
}
out
.
println
(
"</form>"
);
}
}
...
...
This diff is collapsed.
Click to expand it.
WEB-INF/src/controleurs/FollowThread.java
+
39
−
2
View file @
27d83f6d
...
...
@@ -2,16 +2,19 @@ package controleurs;
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
;
import
java.io.IOException
;
import
java.sql.SQLException
;
import
java.util.List
;
@WebServlet
(
"/FollowThread"
)
public
class
FollowThread
{
public
class
FollowThread
extends
HttpServlet
{
public
void
service
(
HttpServletRequest
req
,
HttpServletResponse
res
)
throws
IOException
{
if
(
req
.
getSession
().
getAttribute
(
"user"
)
!=
null
)
{
...
...
@@ -20,12 +23,46 @@ public class FollowThread {
UserDAO
userDao
=
new
UserDAO
();
ThreadDAO
threadDao
=
new
ThreadDAO
();
List
<
MyThread
>
threadsFollowed
=
null
;
try
{
userDao
.
followThread
(
user
,
threadDao
.
getThreadById
((
Integer
.
parseInt
(
req
.
getParameter
(
"threadid"
)))));
threadsFollowed
=
userDao
.
getThreadsFollowedByUser
(
user
);
}
catch
(
SQLException
sqle
)
{
sqle
.
getStackTrace
();
}
boolean
followed
=
false
;
try
{
if
(
threadsFollowed
.
contains
(
threadDao
.
getThreadById
((
Integer
.
parseInt
(
req
.
getParameter
(
"threadid"
))))))
{
followed
=
true
;
}
else
{
followed
=
false
;
}
}
catch
(
SQLException
sqle
)
{
sqle
.
getStackTrace
();
}
if
(!
followed
)
{
try
{
userDao
.
followThread
(
user
,
threadDao
.
getThreadById
((
Integer
.
parseInt
(
req
.
getParameter
(
"threadid"
)))));
res
.
sendRedirect
(
"http://localhost:8080/s4a021-web-backend/Discover"
);
}
catch
(
SQLException
sqle
)
{
sqle
.
getStackTrace
();
}
}
else
{
try
{
userDao
.
unfollowThread
(
user
,
threadDao
.
getThreadById
((
Integer
.
parseInt
(
req
.
getParameter
(
"threadid"
)))));
res
.
sendRedirect
(
"http://localhost:8080/s4a021-web-backend/Discover"
);
}
catch
(
SQLException
sqle
)
{
sqle
.
getStackTrace
();
}
}
}
else
{
res
.
sendRedirect
(
"http://localhost:8080/s4a021-web-backend/Welcome"
);
...
...
This diff is collapsed.
Click to expand it.
WEB-INF/src/dao/UserDAO.java
+
13
−
0
View file @
27d83f6d
...
...
@@ -165,6 +165,19 @@ public class UserDAO {
}
}
public
void
unfollowThread
(
User
user
,
MyThread
thread
)
throws
SQLException
{
PreparedStatement
ps
=
this
.
con
.
prepareStatement
(
"DELETE FROM follow WHERE userID_follow = ? AND threadID_follow = ?)"
);
try
{
ps
.
setInt
(
1
,
user
.
getId
());
ps
.
setInt
(
2
,
thread
.
getId
());
ps
.
executeUpdate
();
}
catch
(
SQLException
sqle
)
{
sqle
.
getStackTrace
();
}
}
// Poster un message dans un thread
public
void
postMessage
(
User
user
,
MyThread
thread
,
String
message
)
throws
SQLException
{
PreparedStatement
ps
=
this
.
con
.
prepareStatement
(
"INSERT INTO msg (userID_msg, threadID, msg, posted_at) VALUES(?, ?, ?, ?)"
);
...
...
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