BrightSide Workbench Full Report + Source Code
Banned.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2023 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.alliance.provider;
20 
21 import java.util.List;
22 import org.turro.alliance.db.AlliancePU;
23 import org.turro.alliance.db.entities.RelationId;
24 import org.turro.jpa.Dao;
25 import org.turro.server.db.entities.AxProjectBan;
26 import org.turro.sql.SqlClause;
27 import org.turro.util.Cached;
28 import org.turro.ws.WsMember;
29 
34 public class Banned {
35 
36  public List<String> getBannedIds() {
37  return SqlClause.select("b").from("AxProjectBan b")
38  .where().equal("b.relation.relatedMemberId", member.getMemberId())
39  .and().isTrue("b.banned")
40  .dao(dao.get())
41  .stream(AxProjectBan.class)
42  .map(b -> b.getId())
43  .toList();
44  }
45 
46  public boolean status(String axId) {
47  AxProjectBan ban = SqlClause.select("b").from("AxProjectBan b")
48  .where().equal("b.relation.relatedMemberId", member.getMemberId())
49  .and().equal("id", axId)
50  .dao(dao.get())
51  .singleResult(AxProjectBan.class);
52  return ban != null ? ban.isBanned() : false;
53  }
54 
55  public boolean toggle(String axId) {
56  AxProjectBan ban = SqlClause.select("b").from("AxProjectBan b")
57  .where().equal("b.relation.relatedMemberId", member.getMemberId())
58  .and().equal("id", axId)
59  .dao(dao.get())
60  .singleResult(AxProjectBan.class);
61  if(ban == null) {
62  ban = new AxProjectBan();
63  ban.setRelation(new RelationId());
64  ban.setId(axId);
67  ban.setBanned(true);
68  } else {
69  ban.setBanned(!ban.isBanned());
70  }
71  ban = dao.get().saveObject(ban);
72  return ban.isBanned();
73  }
74 
75  /* Dao */
76 
77  private final Cached<Dao> dao = Cached.instance(() -> new AlliancePU());
78 
79  /* Factory */
80 
81  public static Banned of(WsMember member) {
82  return new Banned(member);
83  }
84 
85  private final WsMember member;
86 
87  private Banned(WsMember member) {
88  this.member = member;
89  }
90 
91 }
void setRelatedMemberId(Long relatedMemberId)
Definition: RelationId.java:70
void setRelatedEntityId(String relatedEntityId)
Definition: RelationId.java:62
boolean toggle(String axId)
Definition: Banned.java:55
boolean status(String axId)
Definition: Banned.java:46
List< String > getBannedIds()
Definition: Banned.java:36
static Banned of(WsMember member)
Definition: Banned.java:81
void setRelation(RelationId relation)