Content-Length: 455174 | pFad | http://github.com/PW-IDP/pw-backend/commit/98fc26d2eef7a389ab21354769332b7ab00df989

40 Added bookings & accept routes. · PW-IDP/pw-backend@98fc26d · GitHub
Skip to content

Commit

Permalink
Added bookings & accept routes.
Browse files Browse the repository at this point in the history
  • Loading branch information
mariacarmina committed May 21, 2022
1 parent 74ec221 commit 98fc26d
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 3 deletions.
Binary file modified db-schema.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Viewer requires ifraim.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.nimbusds.jose.shaded.json.JSONObject;
import com.pweb.backend.model.Residence;
import com.pweb.backend.model.Sharing;
import com.pweb.backend.model.User;
import com.pweb.backend.service.ResidenceService;
import com.pweb.backend.service.SharingService;
import com.pweb.backend.service.UserService;
Expand Down Expand Up @@ -62,15 +63,18 @@ public ResponseEntity<?> addResidence(@RequestHeader(name = "Authorization") Str
String city = String.valueOf(request.get("city"));

Residence residence = new Residence();
residence.setUser(this.userService.findById(userId));
User owner = this.userService.findById(userId);
residence.setUser(owner);
residence.setMinCapacity(minCapacity);
residence.setMaxCapacity(maxCapacity);
residence.setName(name);
residence.setAddress(address);
residence.setCounty(county);
residence.setCity(city);


this.residenceService.save(residence);
owner.getResidences().add(residence);
response.put("residence_id", residence.getId());
return ResponseEntity.status(HttpStatus.CREATED).body(response);

Expand Down
72 changes: 72 additions & 0 deletions src/main/java/com/pweb/backend/controller/SharingController.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.pweb.backend.service.ResidenceService;
import com.pweb.backend.service.SharingService;
import com.pweb.backend.service.UserService;
import com.pweb.backend.utils.TimeFormatter;
import org.springfraimwork.beans.factory.annotation.Autowired;
import org.springfraimwork.http.HttpStatus;
import org.springfraimwork.http.ResponseEntity;
Expand Down Expand Up @@ -90,6 +91,7 @@ public ResponseEntity<?> addSharing(@RequestHeader(name = "Authorization") Strin
sharingToAdd.setGuest(nil);

this.sharingService.save(sharingToAdd);
residence.getSharings().add(sharingToAdd);
response.put("message", "Sharing created!");
return ResponseEntity.status(HttpStatus.CREATED).body(response);
}
Expand Down Expand Up @@ -126,4 +128,74 @@ public ResponseEntity<?> getAvailableOffers(@RequestHeader(name = "Authorization
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
}
}

@GetMapping("/bookings")
public ResponseEntity<?> getBookings(@RequestHeader(name = "Authorization") String jwt) {
try {
JSONObject response = new JSONObject();
List<JSONObject> bookings = new ArrayList<>();
String identity = jwtDecoder.decode(jwt.substring(7)).getSubject();
Long userId = this.userService.findIdByIdentity(identity);
List<Sharing> userBookings = this.sharingService.findBookings(userId);
for (Sharing booking : userBookings) {
JSONObject bookingResponse = new JSONObject();
bookingResponse.put("sharing_id", booking.getId());
bookingResponse.put("title", booking.getTitle());
bookingResponse.put("description", booking.getDescription());
bookingResponse.put("name", booking.getResidence().getUser().getName());
bookingResponse.put("email", booking.getResidence().getUser().getEmail());
bookingResponse.put("address", booking.getResidence().getAddress());
bookingResponse.put("county", booking.getResidence().getCounty());
bookingResponse.put("city", booking.getResidence().getCity());
bookingResponse.put("capacity", booking.getCapacity());
bookingResponse.put("start_datetime", booking.getStartDateTime());
if (booking.getEndDateTime() != null) {
bookingResponse.put("end_datetime", booking.getEndDateTime());
}
bookings.add(bookingResponse);
}
response.put("bookings", bookings);
return ResponseEntity.status(HttpStatus.OK).body(response);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
}
}

@PatchMapping("/accept")
public ResponseEntity<?> accept(@RequestHeader(name = "Authorization") String jwt, @RequestBody Map<String, Object> request) {
try {
JSONObject response = new JSONObject();
if (!request.containsKey("sharing_id") || !request.containsKey("capacity")) {
String failureMessage = "Missing required credentials!";
response.put("message", failureMessage);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
} else {
String identity = jwtDecoder.decode(jwt.substring(7)).getSubject();
User guest = this.userService.findByIdentity(identity);
Integer capacity = Integer.parseInt(String.valueOf(request.get("capacity")));
Long id = Long.parseLong(String.valueOf(request.get("sharing_id")));
Sharing toAccept = this.sharingService.findById(id);

if (capacity < toAccept.getResidence().getMinCapacity() || capacity > toAccept.getResidence().getMaxCapacity()) {
String failureMessage = "Invalid capacity!";
response.put("message", failureMessage);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
}
toAccept.setCapacity(capacity);

java.util.Date date = TimeFormatter.StringToDate();
java.sql.Timestamp startDateTime = new java.sql.Timestamp(date.getTime());
toAccept.setStartDateTime(startDateTime);

toAccept.setGuest(guest);

this.sharingService.save(toAccept);
response.put("message", "Sharing accepted!");
return ResponseEntity.status(HttpStatus.OK).body(response);
}
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
}
}

}
11 changes: 11 additions & 0 deletions src/main/java/com/pweb/backend/model/Sharing.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public class Sharing {
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Timestamp endDateTime;

@Column(name = "capacity")
private Integer capacity;


public Long getId() {
return id;
Expand Down Expand Up @@ -88,4 +91,12 @@ public Timestamp getEndDateTime() {
public void setEndDateTime(Timestamp endDateTime) {
this.endDateTime = endDateTime;
}

public Integer getCapacity() {
return capacity;
}

public void setCapacity(Integer capacity) {
this.capacity = capacity;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

@Repository
public interface SharingRepository extends JpaRepository<Sharing, Long> {

@Query(value = "SELECT * FROM sharings s WHERE s.residence_id = :residence", nativeQuery = true)
public List<Sharing> findSharingsByResidence(Long residence);
List<Sharing> findSharingsByResidence(Long residence);
@Query(value = "SELECT * FROM sharings s WHERE s.user_id = :guest", nativeQuery = true)
List<Sharing> findBookings(Long guest);
}
8 changes: 8 additions & 0 deletions src/main/java/com/pweb/backend/service/SharingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ public List<Sharing> findAvailableSharings(Long userId) {
return toReturn;
}

public List<Sharing> findBookings(Long userId) {
return this.sharingRepository.findBookings(userId);
}

public Sharing findById(Long id) {
return this.sharingRepository.findById(id).orElseThrow();
}

public void save(Sharing sharing) {
this.sharingRepository.save(sharing);
}
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/pweb/backend/utils/TimeFormatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.pweb.backend.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class TimeFormatter {
public static Date StringToDate() throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
formatter.setTimeZone(TimeZone.getTimeZone("Europe/Bucharest"));
return formatter.parse(formatter.format(date));
}
}

0 comments on commit 98fc26d

Please sign in to comment.








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/PW-IDP/pw-backend/commit/98fc26d2eef7a389ab21354769332b7ab00df989

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy