Skip to content

Commit

Permalink
added all api requests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonrxu committed Apr 12, 2024
1 parent 47eeed4 commit ce9e9ab
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 53 deletions.
11 changes: 11 additions & 0 deletions sublet/interfaces/Offer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// interfaces/Offers.ts

export interface OfferInterface {
id?: number;
phone_number: string;
email?: string;
message?: string;
created_date?: Date;
user?: string;
sublet: number;
}
2 changes: 1 addition & 1 deletion sublet/interfaces/Property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ export interface PropertyInterface {
negotiable: boolean;
start_date: string;
end_date: string;
expires_at: string;
expires_at?: string;
images: ImageInterface[];
}
69 changes: 63 additions & 6 deletions sublet/services/propertyService.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,83 @@
import { PropertyInterface } from '../interfaces/Property';
import apiRequest from './fetch';
import { OfferInterface } from '@/interfaces/Offer';
import apiRequest from '../utils/fetch';

//listSublets
export const fetchProperties = async (): Promise<PropertyInterface[]> => {
const properties: PropertyInterface[] = await apiRequest("properties", "GET");
return properties;
};

//createSublet
export const createProperty = async (property: any): Promise<any> => {
const newProperty: PropertyInterface = await apiRequest("properties", "POST", property);
return newProperty
};

//retrieveSubletSerializerRead
export const fetchProperty = async (id: string): Promise<any> => {
const property: PropertyInterface = await apiRequest(`properties/${id}`, "GET");
return property
};

//updateSublet
export const updateProperty = async (id: string, property: any): Promise<any> => {
const updatedProperty: PropertyInterface = await apiRequest(`properties/${id}`, "PUT", property);
return updatedProperty
};

//partialUpdateSublet
export const partialUpdateProperty = async (id: string, property: any): Promise<any> => {
const updatedProperty: PropertyInterface = await apiRequest(`properties/${id}`, "PATCH", property);
return updatedProperty
};

//destroySublet
export const deleteProperty = async (id: string): Promise<void> => {
await apiRequest(`properties/${id}`, "DELETE");
};

//listAmenitys
export const fetchAmenities = async (): Promise<string[]> => {
const amenities: string[] = await apiRequest("amenities", "GET");
return amenities;
};

//listSubletSerializerSimples
export const fetchFavorites = async (): Promise<PropertyInterface[]> => {
const properties: PropertyInterface[] = await apiRequest("favorites", "GET");
return properties;
};

//listOffers
export const fetchOffers = async (): Promise<OfferInterface[]> => {
const properties: OfferInterface[] = await apiRequest("offers", "GET");
return properties;
};

//listOffers (property)
export const fetchPropertyOffers = async (sublet_id: string): Promise<OfferInterface[]> => {
const properties: OfferInterface[] = await apiRequest(`properties/${sublet_id}/offers`, "GET");
return properties;
};

//createOffer
export const createOffer = async (sublet_id: string, offer: OfferInterface): Promise<any> => {
const newOffer: OfferInterface = await apiRequest(`properties/${sublet_id}/offers`, "POST", offer);
return newOffer
};

//destroyOffer
export const deleteOffer = async (sublet_id: string): Promise<void> => {
await apiRequest(`properties/${sublet_id}/offers`, "DELETE");
};

//createSubletImage
export const createPropertyImage = async (sublet_id: string, image: File): Promise<any> => {
// Create FormData object to hold the image data
const formData = new FormData();
formData.append('sublet', sublet_id); // The 'sublet' field must be an integer
formData.append('images', image); // 'image' should be the File object

return await apiRequest(`properties/${sublet_id}/images`, 'POST', formData)
};

export const fetchAmenities = async (): Promise<string[]> => {
const amenities: string[] = await apiRequest("amenities", "GET");
return amenities;
};
2 changes: 1 addition & 1 deletion sublet/services/fetch.ts → sublet/utils/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import getCsrfTokenCookie from '../utils/csrf';
import getCsrfTokenCookie from './csrf';

/**
* Generic API request function.
Expand Down
45 changes: 0 additions & 45 deletions sublet/utils/fetch.tsx

This file was deleted.

0 comments on commit ce9e9ab

Please sign in to comment.