Skip to content
Snippets Groups Projects
Commit 4c074987 authored by Sarthak Shrivastava's avatar Sarthak Shrivastava
Browse files

Added OrganizationRoster support for owners

parent e497e747
No related branches found
No related tags found
No related merge requests found
...@@ -43,12 +43,23 @@ CREATE TABLE IF NOT EXISTS ORGANIZATION_ROSTER ( ...@@ -43,12 +43,23 @@ CREATE TABLE IF NOT EXISTS ORGANIZATION_ROSTER (
roster_id INT AUTO_INCREMENT NOT NULL, roster_id INT AUTO_INCREMENT NOT NULL,
user_email VARCHAR(128) NOT NULL, user_email VARCHAR(128) NOT NULL,
organization_id INT NOT NULL, organization_id INT NOT NULL,
type ENUM('MEMBER', 'MANAGER') NOT NULL, type ENUM('MEMBER', 'MANAGER', 'OWNER') NOT NULL,
PRIMARY KEY (roster_id), PRIMARY KEY (roster_id),
CONSTRAINT fk_user_manager FOREIGN KEY (user_email) REFERENCES USER (email), CONSTRAINT fk_user_manager FOREIGN KEY (user_email) REFERENCES USER (email),
CONSTRAINT fk_organization_manager FOREIGN KEY (organization_id) REFERENCES ORGANIZATION (organization_id) CONSTRAINT fk_organization_manager FOREIGN KEY (organization_id) REFERENCES ORGANIZATION (organization_id)
); );
INSERT INTO ORGANIZATION_ROSTER (user_email, organization_id, type)
SELECT owner_email, organization_id, 'OWNER'
FROM ORGANIZATION;
SELECT * FROM ORGANIZATION_ROSTER WHERE ORGANIZATION_ROSTER.user_email LIKE 'emilyjohnson@example.com';
SELECT DISTINCT o.*
FROM ORGANIZATION o
JOIN ORGANIZATION_ROSTER r ON o.organization_id = r.organization_id
WHERE r.user_email = 'emilyjohnson@example.com'
OR o.owner_email = 'emilyjohnson@example.com';
CREATE TABLE IF NOT EXISTS REQUEST ( CREATE TABLE IF NOT EXISTS REQUEST (
request_id INT AUTO_INCREMENT NOT NULL, request_id INT AUTO_INCREMENT NOT NULL,
user_email VARCHAR(128) NOT NULL, user_email VARCHAR(128) NOT NULL,
......
package com.example.accessingdatamysql.myorg;
import com.example.accessingdatamysql.org.Organization;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.persistence.Query;
import jakarta.persistence.TypedQuery;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;
@Repository
public class MyOrgRosterRepository implements OrgRosterRepository{
@Autowired
private EntityManager entityManager;
@Transactional
public List<Organization> findUserOrgs(String userEmail)
{
String nativeQuery = "SELECT DISTINCT o.* " +
"FROM ORGANIZATION o " +
"JOIN ORGANIZATION_ROSTER r ON o.organization_id = r.organization_id " +
"WHERE r.user_email = :userEmail OR o.owner_email = :userEmail";
List<Organization> organizations = entityManager
.createNativeQuery(nativeQuery, Organization.class)
.setParameter("userEmail", userEmail)
.getResultList();
return organizations;
}
@Override
public <S extends OrganizationRoster> S save(S entity) {
return null;
}
@Override
public <S extends OrganizationRoster> Iterable<S> saveAll(Iterable<S> entities) {
return null;
}
@Override
public Optional<OrganizationRoster> findById(Integer integer) {
return Optional.empty();
}
@Override
public boolean existsById(Integer integer) {
return false;
}
@Override
@Transactional
public List<OrganizationRoster> findAll() {
TypedQuery<OrganizationRoster> query = entityManager.createQuery("SELECT o FROM Organization o", OrganizationRoster.class);
return query.getResultList();
}
@Override
public Iterable<OrganizationRoster> findAllById(Iterable<Integer> integers) {
return null;
}
@Override
public long count() {
return 0;
}
@Override
public void deleteById(Integer integer) {
}
@Override
public void delete(OrganizationRoster entity) {
}
@Override
public void deleteAllById(Iterable<? extends Integer> integers) {
}
@Override
public void deleteAll(Iterable<? extends OrganizationRoster> entities) {
}
@Override
public void deleteAll() {
}
}
package com.example.accessingdatamysql.myorg;
import com.example.accessingdatamysql.org.Organization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
@CrossOrigin
@RestController // This means that this class is a Controller
@RequestMapping(path="/myorg") // This means URL's start with /orgauth (after Application path)
public class OrgRosterController {
@Autowired // This means to get the bean called userRepository
// Which is auto-generated by Spring, we will use it to handle the data
private OrgRosterRepository orgRosterRepository;
@Autowired
private MyOrgRosterRepository myOrgRosterRepository;
@GetMapping(path="/all")
public @ResponseBody Iterable<OrganizationRoster> getOrgs()
{
return myOrgRosterRepository.findAll();
}
@PostMapping(path="/user")
public @ResponseBody Map<String, Object> getUsersOrgs(@RequestBody Map<String, String> json)
{
Map<String, Object> response = new HashMap<>();
// System.out.println(json.entrySet());
if (json.containsKey("email"))
{
response.put("result", "success");
response.put("data", myOrgRosterRepository.findUserOrgs(json.get("email")));
return response;
}
response.put("result", "failure - bad request");
return response;
}
}
...@@ -5,5 +5,5 @@ import org.springframework.data.repository.CrudRepository; ...@@ -5,5 +5,5 @@ import org.springframework.data.repository.CrudRepository;
// This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository // This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository
// CRUD refers Create, Read, Update, Delete // CRUD refers Create, Read, Update, Delete
public interface OrgRosterRepository extends CrudRepository<Organization_Roster, Integer>{ public interface OrgRosterRepository extends CrudRepository<OrganizationRoster, Integer>{
} }
...@@ -2,12 +2,11 @@ package com.example.accessingdatamysql.myorg; ...@@ -2,12 +2,11 @@ package com.example.accessingdatamysql.myorg;
import jakarta.persistence.*; import jakarta.persistence.*;
import java.io.Serializable;
import java.util.Objects; import java.util.Objects;
@Entity @Entity
@Table(name="Organization_Roster") @Table(name="OrganizationRoster")
public class Organization_Roster { public class OrganizationRoster {
@Id @Id
@GeneratedValue(strategy=GenerationType.IDENTITY) @GeneratedValue(strategy=GenerationType.IDENTITY)
...@@ -25,11 +24,17 @@ public class Organization_Roster { ...@@ -25,11 +24,17 @@ public class Organization_Roster {
this.rosterId = rosterId; this.rosterId = rosterId;
} }
public OrganizationRoster(String userEmail, Integer organizationId, Type type) {
this.userEmail = userEmail;
this.organizationId = organizationId;
this.type = type;
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
Organization_Roster that = (Organization_Roster) o; OrganizationRoster that = (OrganizationRoster) o;
return getRosterId().equals(that.getRosterId()) && userEmail.equals(that.userEmail) && organizationId.equals(that.organizationId) && type == that.type; return getRosterId().equals(that.getRosterId()) && userEmail.equals(that.userEmail) && organizationId.equals(that.organizationId) && type == that.type;
} }
...@@ -38,10 +43,10 @@ public class Organization_Roster { ...@@ -38,10 +43,10 @@ public class Organization_Roster {
return Objects.hash(getRosterId(), userEmail, organizationId, type); return Objects.hash(getRosterId(), userEmail, organizationId, type);
} }
public Organization_Roster() { public OrganizationRoster() {
} }
enum Type { public enum Type {
OWNER, OWNER,
MANAGER, MANAGER,
MEMBER MEMBER
......
package com.example.accessingdatamysql.org; package com.example.accessingdatamysql.org;
import com.example.accessingdatamysql.myorg.OrgRosterRepository;
import com.example.accessingdatamysql.myorg.OrganizationRoster;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
...@@ -12,13 +14,18 @@ public class OrgController { ...@@ -12,13 +14,18 @@ public class OrgController {
// Which is auto-generated by Spring, we will use it to handle the data // Which is auto-generated by Spring, we will use it to handle the data
private OrgRepository orgRepository; private OrgRepository orgRepository;
@Autowired // This means to get the bean called userRepository
// Which is auto-generated by Spring, we will use it to handle the data
private OrgRosterRepository orgRosterRepository;
@PostMapping(path = "/add") // Map ONLY POST Requests @PostMapping(path = "/add") // Map ONLY POST Requests
@ResponseBody @ResponseBody
public Organization addJsonOrg(@RequestBody Organization org) { public Organization addJsonOrg(@RequestBody Organization org) {
// @ResponseBody means the returned String is the response, not a view name // @ResponseBody means the returned String is the response, not a view name
// @RequestParam means it is a parameter from the GET or POST request // @RequestParam means it is a parameter from the GET or POST request
orgRepository.save(org); Organization org2 = orgRepository.save(org);
OrganizationRoster orgRoster = new OrganizationRoster(org2.getOwnerEmail(), org2.getOrgId(), OrganizationRoster.Type.OWNER);
orgRosterRepository.save(orgRoster);
return org; return org;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment