Skip to content

Commit

Permalink
feat: ✨ Configurable media toggle (#167)
Browse files Browse the repository at this point in the history
* feat: ✨ fetch media toggle endpoint+formatting changes

* refactor: ♻️ correct error response type

* feat: ✨ add api to update ug obs customisations
  • Loading branch information
prakhar-s authored Apr 17, 2024
1 parent f5a7b59 commit 02da1da
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/strandls/userGroup/ApiConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,7 @@ public class ApiConstants {
public static final String DATATABLE = "/data-table";
public static final String USERGROUPDATATABLE = "/ud-data-table";
public static final String SPECIES = "/species";
public static final String MEDIATOGGLE = "/mediaToggle";
public static final String OBSERVATIONCUSTOMISATIONS = "/observationCustomisations";

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import com.strandls.userGroup.pojo.FeaturedCreateData;
import com.strandls.userGroup.pojo.GroupGallerySlider;
import com.strandls.userGroup.pojo.GroupHomePageData;
import com.strandls.userGroup.pojo.ObservationCustomisations;
import com.strandls.userGroup.pojo.ReorderingHomePage;
import com.strandls.userGroup.pojo.UserGroup;
import com.strandls.userGroup.pojo.UserGroupAddMemebr;
Expand Down Expand Up @@ -88,7 +89,6 @@ public class UserGroupController {

@Inject
private UserGroupMemberService ugMemberService;

private static final String ERROR_OCCURED_IN_TRANSACTION = "Error occured in transaction";

@GET
Expand All @@ -115,6 +115,47 @@ public Response getUserGroup(@PathParam("objectId") String objectId) {
}
}

@GET
@Path(ApiConstants.MEDIATOGGLE + "/{ugId}")
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Find media toggle value of a UserGroup by ID", notes = "Returns all observations customisation for ug as response", response = ObservationCustomisations.class)
@ApiResponses(value = { @ApiResponse(code = 404, message = "UserGroup not found", response = String.class) })
public Response getUserGroupMediaToggle(@PathParam("ugId") String ugId) {
try {
ObservationCustomisations ugObsCustomisations = ugServices.fetchMediaToggle(Long.parseLong(ugId));
return Response.status(Status.OK).entity(ugObsCustomisations).build();
} catch (Exception e) {
return Response.status(Status.BAD_REQUEST).build();
}
}

@PUT
@Path(ApiConstants.OBSERVATIONCUSTOMISATIONS + ApiConstants.UPDATE)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)

@ValidateUser
@ApiOperation(value = "Update media toggle value of a UserGroup", response = UserGroup.class)
@ApiResponses(value = { @ApiResponse(code = 404, message = "UserGroup not found", response = String.class) })

public Response updateGroupObservationCustomisations(@Context HttpServletRequest request,
@ApiParam(name = "observationCustomisations") ObservationCustomisations observationCustomisations) {
try {
CommonProfile profile = AuthUtil.getProfileFromRequest(request);
JSONArray roles = (JSONArray) profile.getAttribute("roles");
if (roles.contains("ROLE_ADMIN")) {
UserGroup ug = ugServices.updateObservationCustomisations(observationCustomisations);
return Response.status(Status.OK).entity(ug).build();
} else {
return Response.status(Status.FORBIDDEN).build();
}

} catch (Exception e) {
return Response.status(Status.BAD_REQUEST).build();
}
}

@GET
@Path(ApiConstants.IBP + "/{objectId}")
@Consumes(MediaType.TEXT_PLAIN)
Expand Down
25 changes: 20 additions & 5 deletions src/main/java/com/strandls/userGroup/dao/UserGroupDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ public UserGroup findById(Long id) {
}
return entity;
}

@SuppressWarnings("unchecked")
public List<UserGroup> findUgListByIds(List<Long> ids) {
Session session = sessionFactory.openSession();
String qry = "from UserGroup where id IN (:ugIds)" ;

String qry = "from UserGroup where id IN (:ugIds)";
try {
Query<UserGroup> query = session.createQuery(qry);
query.setParameter("ugIds", ids);
return query.getResultList();
return query.getResultList();

} catch (Exception e) {
logger.error(e.getMessage());
Expand All @@ -64,7 +64,6 @@ public List<UserGroup> findUgListByIds(List<Long> ids) {
}
return null;
}


@SuppressWarnings("unchecked")
public List<UserGroup> findFilterRule() {
Expand Down Expand Up @@ -119,4 +118,20 @@ public Long getObservationAuthor(String observationId) {
return null;
}

@SuppressWarnings("unchecked")
public String findMediaToggleByUgId(Long ugId) {
String qry = "SELECT mediaToggle from UserGroup where id = :ugId";
Session session = sessionFactory.openSession();
try {
Query<Object> query = session.createQuery(qry);
query.setParameter("ugId", ugId);
return query.getSingleResult().toString();
} catch (Exception e) {
logger.error(e.getMessage());
} finally {
session.close();
}
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.strandls.userGroup.pojo;

public class ObservationCustomisations {
private Long userGroupId;
private String mediaToggle;

public ObservationCustomisations() {
super();
}

public ObservationCustomisations(Long userGroupId, String mediaToggle) {
super();
this.userGroupId = userGroupId;
this.mediaToggle = mediaToggle;
}

public Long getUserGroupId() {
return userGroupId;
}

public void setUserGroupId(Long userGroupId) {
this.userGroupId = userGroupId;
}

public String getMediaToggle() {
return mediaToggle;
}

public void setMediaToggle(String mediaToggle) {
this.mediaToggle = mediaToggle;
}

}
14 changes: 13 additions & 1 deletion src/main/java/com/strandls/userGroup/pojo/UserGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class UserGroup implements Serializable {
private Boolean showDesc;
private List<Long> habitatIds;
private List<Long> speciesGroupIds;
private String mediaToggle;

/**
*
Expand Down Expand Up @@ -103,7 +104,7 @@ public UserGroup(Long id, Boolean allow_members_to_make_species_call, Boolean al
Date foundedOn, String homePage, String icon, Boolean isDeleted, String name, Double neLatitude,
Double neLongitude, Double swLatitude, Double swLongitude, String theme, Long visitCount, String webAddress,
Long languageId, Date startDate, Boolean showGallery, Boolean showStats, Boolean showRecentObservations,
Boolean showGridMap, Boolean showPartners, Boolean showDesc) {
Boolean showGridMap, Boolean showPartners, Boolean showDesc, String mediaToggle) {
super();
this.id = id;
this.allow_members_to_make_species_call = allow_members_to_make_species_call;
Expand Down Expand Up @@ -132,6 +133,7 @@ public UserGroup(Long id, Boolean allow_members_to_make_species_call, Boolean al
this.showGridMap = showGridMap;
this.showPartners = showPartners;
this.showDesc = showDesc;
this.mediaToggle = mediaToggle;
}

@Id
Expand Down Expand Up @@ -397,4 +399,14 @@ public List<Long> getSpeciesGroupIds() {
public void setSpeciesGroupIds(List<Long> speciesGroupIds) {
this.speciesGroupIds = speciesGroupIds;
}

@Column(name = "media_toggle", columnDefinition = "text default 'withMedia'")
public String getMediaToggle() {
return mediaToggle;
}

public void setMediaToggle(String mediaTogle) {
this.mediaToggle = mediaTogle;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.strandls.userGroup.pojo.FeaturedCreateData;
import com.strandls.userGroup.pojo.GroupGallerySlider;
import com.strandls.userGroup.pojo.GroupHomePageData;
import com.strandls.userGroup.pojo.ObservationCustomisations;
import com.strandls.userGroup.pojo.ReorderingHomePage;
import com.strandls.userGroup.pojo.UserGroup;
import com.strandls.userGroup.pojo.UserGroupAddMemebr;
Expand Down Expand Up @@ -152,4 +153,8 @@ public List<UserGroupIbp> updateUGSpeciesMapping(HttpServletRequest request, Lon

public List<UserGroupIbp> removeUserGroupObervationForDatatable(HttpServletRequest request, Long obvId, Long ugId);

public ObservationCustomisations fetchMediaToggle(Long ugId);

public UserGroup updateObservationCustomisations(ObservationCustomisations updateCustomisationData);

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import com.strandls.userGroup.pojo.GroupGallerySlider;
import com.strandls.userGroup.pojo.GroupHomePageData;
import com.strandls.userGroup.pojo.InvitaionMailData;
import com.strandls.userGroup.pojo.ObservationCustomisations;
import com.strandls.userGroup.pojo.ReorderingHomePage;
import com.strandls.userGroup.pojo.Stats;
import com.strandls.userGroup.pojo.UserGroup;
Expand Down Expand Up @@ -194,6 +195,20 @@ public UserGroup fetchByGroupId(Long id) {
return userGroup;
}

@Override
public ObservationCustomisations fetchMediaToggle(Long ugId) {
try {
ObservationCustomisations customisations = new ObservationCustomisations();
String mediaToggle = userGroupDao.findMediaToggleByUgId(ugId);
customisations.setMediaToggle(mediaToggle);
customisations.setUserGroupId(ugId);
return customisations;
} catch (Exception e) {
logger.error(e.getMessage());
}
return null;
}

@Override
public UserGroupIbp fetchByGroupIdIbp(Long id) {
try {
Expand Down Expand Up @@ -1369,7 +1384,7 @@ public UserGroupIbp createUserGroup(HttpServletRequest request, CommonProfile pr
ugCreateData.getNeLongitude(), ugCreateData.getSwLatitude(), ugCreateData.getSwLongitude(),
ugCreateData.getTheme(), 1L, webAddress,
ugCreateData.getLanguageId() != null ? ugCreateData.getLanguageId() : defaultLanguageId, new Date(),
true, true, true, true, true, true);
true, true, true, true, true, true, "withMedia");

userGroup = userGroupDao.save(userGroup);

Expand Down Expand Up @@ -1463,7 +1478,8 @@ public UserGroupIbp saveUGEdit(HttpServletRequest request, CommonProfile profile
ugEditData.getNeLatitude(), ugEditData.getNeLongitude(), ugEditData.getSwLatitude(),
ugEditData.getSwLongitude(), ugEditData.getTheme(), ug.getVisitCount(), webAddress,
ugEditData.getLanguageId(), new Date(), ug.getShowGallery(), ug.getShowStats(),
ug.getShowRecentObservations(), ug.getShowGridMap(), ug.getShowPartners(), ug.getShowDesc());
ug.getShowRecentObservations(), ug.getShowGridMap(), ug.getShowPartners(), ug.getShowDesc(),
ug.getMediaToggle());

userGroup = userGroupDao.update(userGroup);

Expand Down Expand Up @@ -2267,4 +2283,19 @@ public List<UserGroupIbp> removeUserGroupObervationForDatatable(HttpServletReque
return null;
}

@Override
public UserGroup updateObservationCustomisations(ObservationCustomisations updateCustomisationData) {
try {
Long ugId = updateCustomisationData.getUserGroupId();
String mediaToggle = updateCustomisationData.getMediaToggle();
UserGroup userGroup = userGroupDao.findById(ugId);
userGroup.setMediaToggle(mediaToggle);
return userGroupDao.update(userGroup);
} catch (Exception e) {
logger.error(e.getMessage());
}

return null;
}

}

0 comments on commit 02da1da

Please sign in to comment.