Skip to content

Commit

Permalink
fix article entity and type bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Sojabio committed Dec 12, 2024
1 parent 68a8f9a commit 2d5d748
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 88 deletions.
10 changes: 4 additions & 6 deletions backend/src/entities/article.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ export class Article extends BaseEntity {
@ManyToOne(() => Product, (product) => product.articles)
product: Product;

@Field(() => Reservation, { nullable: true })
@ManyToOne(() => Reservation, (reservation) => reservation.articles, {
nullable: true,
})
reservation: Reservation | null;

@Field(() => [Reservation], { nullable: true }) // GraphQL
@ManyToMany(() => Reservation, reservation => reservation.articles) // TypeORM
reservations?: Reservation[]

}
26 changes: 3 additions & 23 deletions backend/src/resolvers/ArticleResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ class NewArticleInput {
productId: number;
}

@InputType()
class EditArticleInput {
@Field()
availability: boolean;
}

@Resolver(Article)
class ArticleResolver {
@Query(() => [Article])
Expand All @@ -52,20 +46,6 @@ class ArticleResolver {
return newArticle;
}

@Mutation(() => Article)
async editArticle(
@Arg("article") articleId: string,
@Arg("data") newArticleData: EditArticleInput
) {
const article = await Article.findOneByOrFail({
id: Number.parseInt(articleId),
});

article.availability = newArticleData.availability;
const updatedArticle = await article.save();
return updatedArticle;
}

@Authorized(Role.Admin)

@Mutation(() => String)
Expand All @@ -78,18 +58,18 @@ class ArticleResolver {
async deleteArticleFromReservation(@Arg("articleId") articleId: string) {
const article = await Article.findOne({
where: { id: Number.parseInt(articleId) },
relations: { reservation: true },
relations: { reservations: true },
});

if (!article) {
throw new Error("Article not found");
}

if (!article.reservation) {
if (!article.reservations) {
throw new Error("Article is not part of any reservation");
}

article.reservation = null;
article.reservations = [];

await article.save();

Expand Down
51 changes: 1 addition & 50 deletions frontend/src/generated/graphql-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ export type Article = {
__typename?: 'Article';
id: Scalars['Float']['output'];
product: Product;
reservation?: Maybe<Reservation>;
};

export type EditArticleInput = {
availability: Scalars['Boolean']['input'];
reservations?: Maybe<Array<Reservation>>;
};

Expand Down Expand Up @@ -283,14 +278,6 @@ export type DeleteArticleFromReservationMutationVariables = Exact<{

export type DeleteArticleFromReservationMutation = { __typename?: 'Mutation', deleteArticleFromReservation: { __typename?: 'Article', id: number } };

export type EditArticleMutationVariables = Exact<{
data: EditArticleInput;
article: Scalars['String']['input'];
}>;


export type EditArticleMutation = { __typename?: 'Mutation', editArticle: { __typename?: 'Article', id: number, availability: boolean } };

export type HandleReservationMutationVariables = Exact<{
data: NewReservationInput;
}>;
Expand Down Expand Up @@ -349,7 +336,7 @@ export type SearchAndFilterProductsQuery = { __typename?: 'Query', searchAndFilt
export type GetCurrentReservationByUserIdQueryVariables = Exact<{ [key: string]: never; }>;


export type GetCurrentReservationByUserIdQuery = { __typename?: 'Query', getCurrentReservationByUserId: { __typename?: 'ReservationWithTotal', totalPrice: number, reservation: { __typename?: 'Reservation', status: string, startDate: any, endDate: any, id: number, createdAt: any, articles: Array<{ __typename?: 'Article', id: number, product: { __typename?: 'Product', name: string, price: number } }> } } };
export type GetCurrentReservationByUserIdQuery = { __typename?: 'Query', getCurrentReservationByUserId: { __typename?: 'ReservationWithTotal', totalPrice: number, reservation: { __typename?: 'Reservation', status: string, startDate: any, endDate: any, id: number, createdAt: any, articles: Array<{ __typename?: 'Article', id: number, product: { __typename?: 'Product', name: string, price: number, imgUrl: string } }> } } };

export type GetReservationsByArticleIdQueryVariables = Exact<{
articleId: Scalars['String']['input'];
Expand Down Expand Up @@ -675,42 +662,6 @@ export function useDeleteArticleFromReservationMutation(baseOptions?: Apollo.Mut
export type DeleteArticleFromReservationMutationHookResult = ReturnType<typeof useDeleteArticleFromReservationMutation>;
export type DeleteArticleFromReservationMutationResult = Apollo.MutationResult<DeleteArticleFromReservationMutation>;
export type DeleteArticleFromReservationMutationOptions = Apollo.BaseMutationOptions<DeleteArticleFromReservationMutation, DeleteArticleFromReservationMutationVariables>;
export const EditArticleDocument = gql`
mutation EditArticle($data: EditArticleInput!, $article: String!) {
editArticle(data: $data, article: $article) {
id
availability
}
}
`;
export type EditArticleMutationFn = Apollo.MutationFunction<EditArticleMutation, EditArticleMutationVariables>;

/**
* __useEditArticleMutation__
*
* To run a mutation, you first call `useEditArticleMutation` within a React component and pass it any options that fit your needs.
* When your component renders, `useEditArticleMutation` returns a tuple that includes:
* - A mutate function that you can call at any time to execute the mutation
* - An object with fields that represent the current status of the mutation's execution
*
* @param baseOptions options that will be passed into the mutation, supported options are listed on: https://www.apollographql.com/docs/react/api/react-hooks/#options-2;
*
* @example
* const [editArticleMutation, { data, loading, error }] = useEditArticleMutation({
* variables: {
* data: // value for 'data'
* article: // value for 'article'
* },
* });
*/
export function useEditArticleMutation(baseOptions?: Apollo.MutationHookOptions<EditArticleMutation, EditArticleMutationVariables>) {
const options = {...defaultOptions, ...baseOptions}
return Apollo.useMutation<EditArticleMutation, EditArticleMutationVariables>(EditArticleDocument, options);
}
export type EditArticleMutationHookResult = ReturnType<typeof useEditArticleMutation>;
export type EditArticleMutationResult = Apollo.MutationResult<EditArticleMutation>;
export type EditArticleMutationOptions = Apollo.BaseMutationOptions<EditArticleMutation, EditArticleMutationVariables>;

export const HandleReservationDocument = gql`
mutation HandleReservation($data: NewReservationInput!) {
handleReservation(data: $data) {
Expand Down
9 changes: 0 additions & 9 deletions frontend/src/graphql/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,6 @@ export const DELETE_ARTICLE_FROM_RESERVATION = gql`
}
`;

export const EDIT_ARTICLE = gql`
mutation EditArticle($data: EditArticleInput!, $article: String!) {
editArticle(data: $data, article: $article) {
id
availability
}
}
`;

export const HANDLE_RESERVATION = gql`
mutation HandleReservation($data: NewReservationInput!) {
handleReservation(data: $data) {
Expand Down

0 comments on commit 2d5d748

Please sign in to comment.