Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ appsettings.json
*.userprefs

# Mono auto generated files
mono_crash.*
mono_crash.*\

.idea/

# Build results
[Dd]ebug/
Expand Down
Binary file added ERD.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace exercise.wwwapi.Configuration;

public class ConfigurationSettings : IConfigurationSettings
{
private readonly IConfiguration _configuration;

public ConfigurationSettings()
{
_configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").AddUserSecrets<Program>().Build();
}

public string GetValue(string key)
{
return _configuration.GetValue<string>(key)!;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace exercise.wwwapi.Configuration;

public interface IConfigurationSettings
{
string? GetValue(string key);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Text.Json.Serialization;

namespace api_cinema_challenge.DTOs.Customer;

public class CustomerPut
{
[JsonPropertyName("name")]
public string Name { get; set; }

[JsonPropertyName("email")]
public string Email { get; set; }

[JsonPropertyName("phone")]
public string Phone { get; set; }
}
27 changes: 27 additions & 0 deletions api-cinema-challenge/api-cinema-challenge/DTOs/Movie/MoviePost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Text.Json.Serialization;

namespace api_cinema_challenge.DTOs.Movie;

public class MoviePost
{
[JsonPropertyName("id")]
public int Id { get; set; }

[JsonPropertyName("title")]
public string Title { get; set; }

[JsonPropertyName("rating")]
public string Rating { get; set; }

[JsonPropertyName("description")]
public string Description { get; set; }

[JsonPropertyName("runtimeMins")]
public int RuntimeMins { get; set; }

[JsonPropertyName("createdAt")]
public DateTime CreatedAt { get; set; }

[JsonPropertyName("updatedAt")]
public DateTime UpdatedAt { get; set; }
}
18 changes: 18 additions & 0 deletions api-cinema-challenge/api-cinema-challenge/DTOs/Movie/MoviePut.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Text.Json.Serialization;

namespace api_cinema_challenge.DTOs.Movie;

public class MoviePut
{
[JsonPropertyName("title")]
public string Title { get; set; }

[JsonPropertyName("rating")]
public string Rating { get; set; }

[JsonPropertyName("description")]
public string Description { get; set; }

[JsonPropertyName("runtimeMins")]
public int RuntimeMins { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Text.Json.Serialization;

namespace api_cinema_challenge.DTOs.Screening;

public class ScreeningPost
{
[JsonPropertyName("id")]
public int Id { get; set; }

[JsonPropertyName("screenNumber")]
public int ScreenNumber { get; set; }

[JsonPropertyName("capacity")]
public int Capacity { get; set; }

[JsonPropertyName("startsAt")]
public DateTime StartsAt { get; set; }

[JsonPropertyName("createdAt")]
public DateTime CreatedAt { get; set; }

[JsonPropertyName("updatedAt")]
public DateTime UpdatedAt { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Text.Json.Serialization;

namespace api_cinema_challenge.DTOs.Screening;

public class ScreeningPut
{
[JsonPropertyName("screenNumber")]
public int ScreenNumber { get; set; }

[JsonPropertyName("capacity")]
public int Capacity { get; set; }

[JsonPropertyName("startsAt")]
public DateTime StartsAt { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Text.Json.Serialization;

namespace api_cinema_challenge.DTOs.User.Login;

public class LoginRequestDTO
{
[JsonPropertyName("username")]
public string Username { get; set; }

[JsonPropertyName("password")]
public string Password { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Text.Json.Serialization;

namespace api_cinema_challenge.DTOs.User.Login;

public class LoginSuccessDTO
{
[JsonPropertyName("token")]
public string Token { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Text.Json.Serialization;

namespace api_cinema_challenge.DTOs.User.Register;

public class RegisterRequestDTO
{
[JsonPropertyName("username")]
public string Username { get; set; }

[JsonPropertyName("password")]
public string Password { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Text.Json.Serialization;

namespace api_cinema_challenge.DTOs.User.Register;

public class RegisterSuccessDTO
{
[JsonPropertyName("username")]
public string Username { get; set; }
}
26 changes: 0 additions & 26 deletions api-cinema-challenge/api-cinema-challenge/Data/CinemaContext.cs

This file was deleted.

53 changes: 53 additions & 0 deletions api-cinema-challenge/api-cinema-challenge/Data/DataContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using api_cinema_challenge.Models;
using Microsoft.EntityFrameworkCore;

namespace api_cinema_challenge.Data
{
public sealed class DataContext : DbContext
{
private readonly string _connectionString;

public DataContext(DbContextOptions<DataContext> options) : base(options)
{
var configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
_connectionString = configuration.GetValue<string>("ConnectionStrings:DefaultConnectionString")!;
Database.EnsureCreated();
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// optionsBuilder.UseNpgsql(_connectionString);
optionsBuilder.UseInMemoryDatabase("Database");
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>().HasData(new Customer
{
Id = 1, Name = "John Doe", Email = "b@b.nl", Phone = "phonenumber", CreatedAt = DateTime.MinValue,
UpdatedAt = DateTime.MinValue
});

modelBuilder.Entity<Movie>().HasData(
new Movie
{
Id = 1, CreatedAt = DateTime.MinValue, Description = "DESCRIPTION", Rating = "10/10",
RuntimeMins = 5, Title = "title", UpdatedAt = DateTime.MinValue
}
);

modelBuilder.Entity<Screening>().HasData(new Screening
{
Id = 1, CreatedAt = DateTime.MinValue, UpdatedAt = DateTime.MinValue, Capacity = 5, MovieId = 1,
ScreenNumber = 1, StartsAt = DateTime.MinValue,
});

modelBuilder.Entity<User>().HasData(new User
{
Id = 1,
Username = "johndoe",
PasswordHash = "$2a$11$1Cp4pRvdnJ7YDRQmBW9qme1fBLmRI1OcZCK6RG0.CpiSZH1Fk5LsK",
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using api_cinema_challenge.DTOs.Customer;
using api_cinema_challenge.Models;
using exercise.wwwapi.Repository;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace api_cinema_challenge.Endpoints;

public static class CustomerEndpoints
{
public static void ConfigureCustomerEndpoints(this WebApplication app)
{
var customers = app.MapGroup("customers");
customers.MapPost("/", Create).WithDescription("Create a new customer account.");
customers.MapGet("/", GetAll).WithDescription("Get a list of every customer.");
customers.MapPatch("/{id:int}", Update).WithDescription(
"Update an existing customer. For ease of implementation, all fields are required from the client.");
customers.MapDelete("/{id:int}", Delete).WithDescription(
"Delete an existing customer. When deleting data, it's useful to send the deleted record back to the client so they can re-create it if deletion was a mistake.\n");
}

[Authorize]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
private static async Task<IResult> Create(IRepository<Customer> customerRepository, CustomerPut customerPut)
{
var createTime = DateTime.UtcNow;
var customer = new Customer
{
Email = customerPut.Email,
Name = customerPut.Name,
Phone = customerPut.Phone,
CreatedAt = createTime,
UpdatedAt = createTime,
};
customerRepository.Insert(customer);
await customerRepository.SaveAsync();
return Results.Created("/", customer);
}

[Authorize]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
private static async Task<IResult> GetAll(IRepository<Customer> customerRepository)
{
var result = await customerRepository.GetAllAsync();

var resultArray = result.ToArray();
if (resultArray.Length == 0)
{
return Results.NoContent();
}

return Results.Ok(resultArray);
}

[Authorize]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
private static async Task<IResult> Update(IRepository<Customer> customerRepository, int id, CustomerPut customerPut)
{
var customer = await customerRepository.GetByIdAsync(id);
if (customer == null)
{
return Results.NotFound("Customer not found");
}

customer.Name = customerPut.Name;
customer.Phone = customerPut.Phone;
customer.Email = customerPut.Email;
customer.UpdatedAt = DateTime.UtcNow;

customerRepository.Update(customer);
await customerRepository.SaveAsync();

return Results.Ok(customer);
}

[Authorize]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
private static async Task<IResult> Delete(IRepository<Customer> customerRepository, int id)
{
var customer = await customerRepository.GetByIdAsync(id);
if (customer == null)
{
return Results.NotFound("Customer not found");
}

customerRepository.Delete(customer);
await customerRepository.SaveAsync();

return Results.Ok(customer);
}
}
Loading