Skip to content

Commit

Permalink
Merge branch 'mic'
Browse files Browse the repository at this point in the history
  • Loading branch information
ML-Chen committed Apr 29, 2020
2 parents d0c42c3 + 0e98969 commit 6edc224
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 5 deletions.
2 changes: 1 addition & 1 deletion backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ app.post("/api/login", apiController.login);
app.post("/api/listings/new", apiController.newListing);
app.get("/api/listings/nearby", apiController.getNearby);
app.post("/api/users/new", apiController.newUser);
app.get("/api/users/:id/listings", apiController.getListingsByHostId);
app.get("/api/users/:id/rentals", apiController.getRentalHistory);
app.get("/api/users/:id/lendings", apiController.getLendingHistory);
app.post("/api/listings/:id/rent", apiController.rentListing);
app.get("/api/listings/:id", apiController.getListing);


export default app;
15 changes: 14 additions & 1 deletion backend/src/controllers/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export const getRentalHistory = async (req: Request, res: Response) => {
export const getLendingHistory = async (req: Request, res: Response) => {
try {
const history = await (Rental as unknown as RentalDocument).listLenderHistory(req.params.id);
res.json(history.map(rental => {return { ...(rental.toObject()), name: rental.lender.firstName + " " + rental.lender.lastName };}));
res.json(history.map(rental => {return { ...(rental.toObject()), name: rental.renter.firstName + " " + rental.renter.lastName };}));
} catch (err) {
res.status(400).send(err);
}
Expand Down Expand Up @@ -198,3 +198,16 @@ export const getNearby = async (req: Request, res: Response) => {
res.status(400).send(err);
}
};

/**
* GET /api/users/:id/listings
* @param {string} req.params.id
*/
export const getListingsByHostId = async (req: Request, res: Response) => {
try {
const listings = await (Listing as any as ListingDocument).getByHostId(req.params.id);
res.json(listings);
} catch (err) {
res.status(400).send(err);
}
};
10 changes: 10 additions & 0 deletions backend/src/models/Listing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type _ListingDocument = {
price: number;
construct: (hostId: any, lat: number, lon: number, capacity: number, startDate: Date, endDate: Date, price: number) => Promise<ListingDocument>;
getNearby: (lat?: number, lon?: number, minCapacity?: number, maxPrice?: number, startDate?: Date, endDate?: Date) => Promise<Array<ListingDocument & { distance: number }>>;
getByHostId: (hostId: string) => Promise<Array<ListingDocument>>;
}

export type ListingDocument = mongoose.Document & _ListingDocument;
Expand Down Expand Up @@ -73,4 +74,13 @@ listingSchema.statics.getNearby = async function (lat: number, lon: number, minC
}
};

listingSchema.statics.getByHostId = async function (userId: string) {
try {
return await this.find({ host: userId }).sort({ endDate: -1 }).exec();
} catch (err) {
console.log(err);
return Promise.reject(err);
}
};

export const Listing = mongoose.model<ListingDocument>("Listing", listingSchema);
2 changes: 1 addition & 1 deletion react_ionic/src/components/Discover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const DiscoverCard: React.FC<DiscoverListing> = ({ price, distance, remSpace, ho
<IonCol col-12 >
<IonImg src={image} alt="Room"/>
<p>Space Available: {moment(startDate).format('ll')} - {moment(endDate).format('ll')}</p>
<p>Contact: {host.phoneNumber || host.email} </p> {/* TODO: Add phone number to discover card */}
<p>Contact: {host.phoneNumber || host.email} </p>

<Link to={`/listing/${_id}`}>
<IonButton expand="full" color="warning" size="default" href={`/listing/${_id}`}>
Expand Down
44 changes: 42 additions & 2 deletions react_ionic/src/components/Profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,38 @@ import {
IonPage,
IonGrid

} from '@ionic/react'
} from '@ionic/react';
import { pin, cube, calendar, person, cash } from 'ionicons/icons';
import { RouteComponentProps } from 'react-router';
import { Link } from 'react-router-dom';

const YourListingCard: React.FC<any> = ({ price, lat, lon, capacity, remSpace, startDate, endDate, _id, image }) => {
return (<>
<IonCard>
<IonItem>
<IonRow>
<IonCol col-12>
<IonCardTitle color="success">${price}/mo per box</IonCardTitle>
<IonCardSubtitle><IonIcon icon={pin}></IonIcon>{`${lat.toFixed(2)}, ${lon.toFixed(2)}`} </IonCardSubtitle>
<IonCardSubtitle><IonIcon icon={cube}></IonIcon> {capacity - remSpace}/{capacity} Boxes</IonCardSubtitle>
</IonCol>
</IonRow>
</IonItem>

<IonCardContent class="ion-no-padding">
<IonRow>
<IonCol col-12 >
{image ? <IonImg src={image} alt="Room"/> : null}
<p>Space Available: {moment(startDate).format('ll')} - {moment(endDate).format('ll')}</p>
</IonCol>
</IonRow>
</IonCardContent>
</IonCard>
</>)
}

const HistoryCard: React.FC<HistoryType> = ({ name, price, boxes, dropoff, pickup }) => {
const current = pickup > new Date();
const current = new Date(pickup) > new Date();

return (<>
<IonCard color={current ? "" : "light"}>
Expand All @@ -51,6 +77,7 @@ const HistoryCard: React.FC<HistoryType> = ({ name, price, boxes, dropoff, picku
Boxes: {boxes} <br />
{moment(dropoff).format('ll')} - {moment(pickup).format('ll')} <br />
Status: {current ? <IonText color="success"><b>Current</b></IonText> : "Past"} <br />
{/* TODO: make cancellation functional */}
{current ? <a href="#"><IonText color="danger"><b>Cancel</b></IonText></a> : ""}
</p>
</IonCol>
Expand All @@ -65,10 +92,15 @@ const HistoryCard: React.FC<HistoryType> = ({ name, price, boxes, dropoff, picku
const Profile: React.FC<RouteComponentProps> = (props) => {
const { state, dispatch } = useContext(AppContext);
const { userId, token, firstName, lastName } = state;
let listingsContent;
let lendingsContent;
let rentalsContent;
const { data: listings, error: errorListings } = useSWR(userId ? `http://localhost:3001/api/users/${userId}/listings` : null, url => wretch(url).get().json());
const { data: lendings, error: errorLendings } = useSWR(userId ? `http://localhost:3001/api/users/${userId}/lendings` : null, url => wretch(url).get().json());
const { data: rentals, error: errorRentals } = useSWR(userId ? `http://localhost:3001/api/users/${userId}/rentals` : null, url => wretch(url).get().json());
if (!errorListings && listings) {
listingsContent = listings.length > 0 ? listings.map(listing => <YourListingCard {...listing} key={listing._id} />) : <IonItem><IonLabel>No listings (yet!)</IonLabel></IonItem>
}
if (!errorRentals && rentals) {
rentalsContent = rentals.length > 0 ? rentals.map(rental => <HistoryCard {...rental} key={rental._id} />) : <IonItem><IonLabel>No rentals (yet!)</IonLabel></IonItem>
}
Expand Down Expand Up @@ -119,6 +151,14 @@ const Profile: React.FC<RouteComponentProps> = (props) => {
{rentalsContent}
</IonCol>
</IonRow>
<IonRow>
<IonCol sizeMd="6" offsetMd="3">
<IonItem>
<IonTitle>Listings History</IonTitle>
</IonItem>
{listingsContent}
</IonCol>
</IonRow>
</IonGrid>

</IonContent>
Expand Down

0 comments on commit 6edc224

Please sign in to comment.