Skip to content

Commit 5343edf

Browse files
committed
2 parents 4134d17 + 62183a3 commit 5343edf

File tree

6 files changed

+265
-154
lines changed

6 files changed

+265
-154
lines changed

frontend/styles/shared-styles.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,10 @@
1010
max-width: 250px;
1111
}
1212

13+
.mygroups {
14+
margin: 0 auto;
15+
max-width: 250px;
16+
/*background-color: #14c7c7;
17+
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='50' height='40' viewBox='0 0 50 40'%3E%3Cg fill-rule='evenodd'%3E%3Cg fill='%23000000' fill-opacity='0.4'%3E%3Cpath d='M40 10L36.67 0h-2.11l3.33 10H20l-2.28 6.84L12.11 0H10l6.67 20H10l-2.28 6.84L2.11 10 5.44 0h-2.1L0 10l6.67 20-3.34 10h2.11l2.28-6.84L10 40h20l2.28-6.84L34.56 40h2.1l-3.33-10H40l2.28-6.84L47.89 40H50l-6.67-20L50 0h-2.1l-5.62 16.84L40 10zm1.23 10l-2.28-6.84L34 28h4.56l2.67-8zm-10.67 8l-2-6h-9.12l2 6h9.12zm-12.84-4.84L12.77 38h15.79l2.67-8H20l-2.28-6.84zM18.77 20H30l2.28 6.84L37.23 12H21.44l-2.67 8zm-7.33 2H16l-4.95 14.84L8.77 30l2.67-8z' /%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
18+
*/}
19+

src/main/java/ie/tcd/pavel/JoinCreateGroupPage.java

Lines changed: 0 additions & 125 deletions
This file was deleted.

src/main/java/ie/tcd/pavel/MainPage.java

Lines changed: 108 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
import com.vaadin.flow.component.Text;
66
import com.vaadin.flow.component.applayout.AppLayout;
77
import com.vaadin.flow.component.applayout.DrawerToggle;
8+
import com.vaadin.flow.component.button.Button;
89
import com.vaadin.flow.component.dependency.CssImport;
9-
import com.vaadin.flow.component.html.Anchor;
10-
import com.vaadin.flow.component.html.H1;
11-
import com.vaadin.flow.component.html.Image;
12-
import com.vaadin.flow.component.html.Span;
10+
import com.vaadin.flow.component.dialog.Dialog;
11+
import com.vaadin.flow.component.html.*;
1312
import com.vaadin.flow.component.orderedlayout.FlexComponent;
1413
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
1514
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
1615
import com.vaadin.flow.component.tabs.Tab;
1716
import com.vaadin.flow.component.tabs.TabVariant;
1817
import com.vaadin.flow.component.tabs.Tabs;
18+
import com.vaadin.flow.component.textfield.TextField;
1919
import com.vaadin.flow.router.BeforeEnterEvent;
2020
import com.vaadin.flow.router.BeforeEnterObserver;
2121
import com.vaadin.flow.router.Route;
@@ -35,9 +35,13 @@
3535
public class MainPage extends AppLayout implements BeforeEnterObserver {
3636

3737
private HashMap<Tab, Component> tabMap = new HashMap<>();
38-
private Image background = new Image("https://i.imgur.com/4AhkxVz.jpg", "Background");
38+
39+
MongoDBOperations database;
40+
private Image background = new Image("https://i.imgur.com/rrlI3vE.jpg", "Background");
41+
3942

4043
public MainPage() {
44+
database = BeanUtil.getBean(MongoDBOperations.class);
4145
this.setContent(background);
4246
setPrimarySection(AppLayout.Section.DRAWER);
4347
}
@@ -52,14 +56,20 @@ public void beforeEnter(BeforeEnterEvent beforeEnterEvent) {
5256
tabs = new Tabs(
5357
createHomeTab(),
5458
createTab("Add Exercise", AddExercisePage.class),
55-
createTab("Create/Join Group", JoinCreateGroupPage.class),
59+
createJoinGroupTab(),
60+
createCreateGroupTab(),
5661
createChartsTab(),
5762
createMyGroupsTab()
5863
);
5964
tabs.addSelectedChangeListener(event -> {
6065
final Tab selectedTab = event.getSelectedTab();
6166
if(tabMap.containsKey(selectedTab)) {
62-
setContent(tabMap.get(selectedTab));
67+
if(!selectedTab.getLabel().equals("Join Group")&&!selectedTab.getLabel().equals("Create Group"))
68+
setContent(tabMap.get(selectedTab));
69+
else
70+
{
71+
((Dialog) tabMap.get(selectedTab)).open();
72+
}
6373
}
6474
});
6575

@@ -84,6 +94,23 @@ public void beforeEnter(BeforeEnterEvent beforeEnterEvent) {
8494
addToDrawer(tabs);
8595
}
8696

97+
private Tab createJoinGroupTab()
98+
{
99+
Tab joinGroup = new Tab("Join Group");
100+
joinGroup.addThemeVariants(TabVariant.LUMO_ICON_ON_TOP);
101+
tabMap.put(joinGroup,createJoinGroupDialog());
102+
return joinGroup;
103+
}
104+
105+
private Tab createCreateGroupTab()
106+
{
107+
Tab createGroup = new Tab("Create Group");
108+
createGroup.addThemeVariants(TabVariant.LUMO_ICON_ON_TOP);
109+
tabMap.put(createGroup,createCreateGroupDialog());
110+
return createGroup;
111+
}
112+
113+
87114
private Tab createHomeTab()
88115
{
89116
Tab home = new Tab("Home");
@@ -149,4 +176,78 @@ private void createHeaderWithoutLogout() {
149176

150177
addToNavbar(header);
151178
}
179+
180+
private Dialog createJoinGroupDialog()
181+
{
182+
Dialog dialog = new Dialog();
183+
TextField joinGroupNameField = new TextField();
184+
TextField joinGroupPassField = new TextField();
185+
joinGroupNameField.getStyle().set("width", "15em");
186+
joinGroupNameField.setMaxLength(13);
187+
joinGroupNameField.setLabel("Group Name: ");
188+
joinGroupPassField.getStyle().set("width", "15em");
189+
joinGroupPassField.setMaxLength(13);
190+
joinGroupPassField.setLabel("Group Password: ");
191+
Label joinErrorLabel = new Label();
192+
Button confirmJoinGroup = new Button("Confirm");
193+
VerticalLayout vL = new VerticalLayout (joinGroupNameField, joinGroupPassField, confirmJoinGroup, joinErrorLabel);
194+
195+
confirmJoinGroup.addClickListener( event -> {
196+
// User is attempting to join a group
197+
if(!joinGroupNameField.getValue().equals("") && !joinGroupPassField.getValue().equals("")) {
198+
if (database.groupExists(joinGroupNameField.getValue(), joinGroupPassField.getValue())) {
199+
database.insertGroup(joinGroupNameField.getValue(), joinGroupPassField.getValue(),
200+
SecurityUtils.getUsername());
201+
dialog.close();
202+
System.out.printf("[DEBUG] Group Joined: Name - %s | Password - %s%n", joinGroupNameField.getValue(), joinGroupPassField.getValue());
203+
} else {
204+
System.out.printf("[DEBUG] Someone tried to join a group that does not exist. Password Incorrect.%n");
205+
}
206+
}
207+
dialog.close();
208+
});
209+
210+
dialog.add(vL);
211+
212+
return dialog;
213+
}
214+
215+
private Dialog createCreateGroupDialog()
216+
{
217+
Dialog dialog = new Dialog();
218+
TextField newGroupNameField = new TextField();
219+
TextField newGroupPassField = new TextField();
220+
newGroupNameField.getStyle().set("width", "15em");
221+
newGroupNameField.setMaxLength(13);
222+
newGroupNameField.setLabel("New Group Name: ");
223+
newGroupPassField.getStyle().set("width", "15em");
224+
newGroupPassField.setMaxLength(13);
225+
newGroupPassField.setLabel("New Group Password: ");
226+
Label createErrorLabel = new Label();
227+
Button confirmCreateGroup = new Button ("Confirm");
228+
VerticalLayout vL = new VerticalLayout (newGroupNameField, newGroupPassField, confirmCreateGroup, createErrorLabel);
229+
230+
confirmCreateGroup.addClickListener( event -> {
231+
// User is creating a new group
232+
if(!newGroupNameField.getValue().equals("") && !newGroupPassField.getValue().equals("")) {
233+
if(!database.groupExists(newGroupNameField.getValue())) {
234+
database.insertGroup(newGroupNameField.getValue(), newGroupPassField.getValue(),
235+
SecurityUtils.getUsername());
236+
database.makeAdmin(SecurityUtils.getUsername(), newGroupNameField.getValue());
237+
dialog.close();
238+
System.out.printf("[DEBUG] Group Created: Name - %s | Password - %s%n", newGroupNameField.getValue(), newGroupPassField.getValue());
239+
} else {
240+
vL.remove(createErrorLabel);
241+
createErrorLabel.setText("Group Name already exists");
242+
vL.add(createErrorLabel);
243+
System.out.printf("[DEBUG] Someone tried to create a group when the name already exists%n");
244+
}
245+
}
246+
dialog.close();
247+
});
248+
249+
dialog.add(vL);
250+
251+
return dialog;
252+
}
152253
}

src/main/java/ie/tcd/pavel/MongoDBOperations.java

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -188,15 +188,32 @@ public void deleteExercise(String owner, String type, Date date)
188188
public void insertGroup(String name, String password, String user)
189189
{
190190
mongoTemplate.insert(new Group(name,user));
191-
mongoTemplate.insert(new GroupPassword(name,encoder.encode(password)));
191+
if(getUsersByGroup(name).size() < 2) {
192+
mongoTemplate.insert(new GroupPassword(name,encoder.encode(password)));
193+
}
192194
}
193195

196+
197+
public Group findGroup(String user, String group)
198+
{
199+
Query searchGroup = new Query(Criteria.where("user").is(user).and("name").is(group));
200+
Group groupResult = mongoTemplate.findOne(searchGroup,Group.class);
201+
return groupResult;
202+
203+
}
204+
205+
194206
public boolean groupExists(String name, String password)
195207
{
196208
Query searchGroupPassword = new Query(Criteria.where("name").is(name));
197209
List<GroupPassword> resultGroups = mongoTemplate.query(GroupPassword.class).matching(searchGroupPassword).all();
198-
System.out.println(resultGroups.get(0).getPassword());
199-
return encoder.matches(password,resultGroups.get(0).getPassword());
210+
if(resultGroups.size()>0) {
211+
return encoder.matches(password, resultGroups.get(0).getPassword());
212+
}
213+
else
214+
{
215+
return false;
216+
}
200217
}
201218

202219
public boolean groupExists(String name)
@@ -298,22 +315,34 @@ else if(exerciseTypes.getTimeExercises().contains(type))
298315
return cumulativeData;
299316
}
300317

318+
public void makeAdmin(String user, String group)
319+
{
320+
mongoTemplate.insert(new GroupAdmin(user,group));
321+
}
301322

323+
public boolean checkIfAdmin(String user, String group)
324+
{
325+
Query checkAdmin = new Query(Criteria.where("admin").is(user).and("groupName").is(group));
326+
List<GroupAdmin> admins = mongoTemplate.query(GroupAdmin.class).matching(checkAdmin).all();
327+
return admins.size()>0;
328+
}
302329

330+
public void removeUserFromGroup(String user, String group)
331+
{
332+
Group groupToRemove = this.findGroup(user,group);
333+
mongoTemplate.remove(groupToRemove);
334+
}
303335

304336

305-
306-
307-
308-
309-
310-
311-
312-
313-
314-
315-
316-
317-
318-
337+
public void deleteGroupFinal(String name)
338+
{
339+
Query findAdmins = new Query(Criteria.where("groupName").is(name));
340+
List<GroupAdmin> admins = mongoTemplate.query(GroupAdmin.class).matching(findAdmins).all();
341+
for(GroupAdmin admin : admins) {
342+
mongoTemplate.remove(admin);
343+
}
344+
Query searchGroupPassword = new Query(Criteria.where("name").is(name));
345+
GroupPassword password = mongoTemplate.findOne(searchGroupPassword,GroupPassword.class);
346+
mongoTemplate.remove(password);
347+
}
319348
}

0 commit comments

Comments
 (0)