Skip to content

Instantly share code, notes, and snippets.

View dsibinski's full-sized avatar

Dawid Sibiński dsibinski

View GitHub Profile
test("should apply 10% discount if there are exactly 5 business class tickets", () => {
const tickets = Array.from({ length: 5 }, () => ({
price: 10,
isBusinessClass: true,
}));
const totalPrice = calculator.calculateTotalTicketsPrice(tickets);
expect(totalPrice).toBe(45); // 10% discount
});
import { Ticket } from "./ticket";
import { TicketsPriceCalculator } from "./ticketsPriceCalculator";
describe("TicketsPriceCalculator", () => {
let calculator: TicketsPriceCalculator;
beforeEach(() => {
calculator = new TicketsPriceCalculator();
});
export class TicketsPriceCalculator {
calculateTotalTicketsPrice(tickets: Ticket[]): number {
if (tickets.length === 0) {
return 0;
}
let totalPrice = tickets.reduce((sum, ticket) => sum + ticket.price, 0);
const businessClassTickets = tickets.filter(
(ticket) => ticket.isBusinessClass
).length;
getAllUsers = async (): Promise<UserViewModel[]> => {
const url = `${this.apiEndpoint}/AllUsers`;
const response = await fetch(url);
const usersJson = await response.json();
const users = z.array(UserViewModelSchema).parse(usersJson);
return users;
};
import { z } from "zod";
import { AddressViewModelSchema } from "./addressViewModel";
export const UserViewModelSchema = z.object({
id: z.string().uuid(),
name: z.string(),
lastName: z.string(),
login: z.string(),
isActive: z.boolean(),
loyaltyPoints: z.number(),
export const UserViewModelSchema = z.object({
id: z.string().uuid(),
name: z.string(),
lastName: z.string(),
login: z.string(),
isActive: z.boolean(),
loyaltyPoints: z.number(),
address: AddressViewModelSchema.nullable(),
});
public record UserViewModel(Guid Id, string Name, string LastName,
string Login, bool IsActive, int FidelityPoints, AddressViewModel? Address = null);
export class UsersService {
private apiEndpoint: string;
constructor() {
this.apiEndpoint = "https://localhost:7131/Users";
}
getAllUsers = async (): Promise<UserViewModel[]> => {
const url = `${this.apiEndpoint}/AllUsers`;
const response = await fetch(url);
export interface UserViewModel {
id: Guid;
name: string;
lastName: string;
login: string;
isActive: boolean;
loyaltyPoints: number;
address: AddressViewModel | null;
}
public record UserViewModel(Guid Id, string Name, string LastName,
string Login, bool IsActive, int LoyaltyPoints, AddressViewModel? Address = null);