Skip to content

ateeb #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

ateeb #105

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
14 changes: 14 additions & 0 deletions exercise.webapi/DTO/AddBookDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using exercise.webapi.Models;

namespace exercise.webapi.DTO
{
public class AddBookDTO
{
public string Title { get; set; }

public int AuthorId { get; set; }

//public Author Author { get; set; }

}
}
13 changes: 13 additions & 0 deletions exercise.webapi/DTO/AuthorDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using exercise.webapi.Models;

namespace exercise.webapi.DTO
{
public class AuthorDTO
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }

}
}
13 changes: 13 additions & 0 deletions exercise.webapi/DTO/AuthorListDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using exercise.webapi.Models;

namespace exercise.webapi.DTO
{
public class AuthorListDTO
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public List<BookListDTO> Books { get; set; } = new List<BookListDTO>();
}
}
13 changes: 13 additions & 0 deletions exercise.webapi/DTO/BookDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using exercise.webapi.Models;

namespace exercise.webapi.DTO
{
public class BookDTO
{
public int Id { get; set; }
public string Title { get; set; }

public int AuthorId { get; set; }
public AuthorDTO Author { get; set; }
}
}
8 changes: 8 additions & 0 deletions exercise.webapi/DTO/BookListDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace exercise.webapi.DTO
{
public class BookListDTO
{
public int Id { get; set; }
public string Title { get; set; }
}
}
7 changes: 7 additions & 0 deletions exercise.webapi/DTO/BookOnlyDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace exercise.webapi.DTO
{
public class BookOnlyDTO
{
public string Title { get; set; }
}
}
127 changes: 127 additions & 0 deletions exercise.webapi/Endpoints/AuthorApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
using exercise.webapi.DTO;
using exercise.webapi.Mapper;
using exercise.webapi.Models;
using exercise.webapi.Repository;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;

namespace exercise.webapi.Endpoints
{
public static class AuthorApi
{
public static void ConfigureAuthorEndpoint(this WebApplication app)
{
var pets = app.MapGroup("authors");

pets.MapGet("/getId{id}", GetAuthor);
pets.MapGet("/getAll", GetAllAuthors);

}

[ProducesResponseType(StatusCodes.Status200OK)]
public static async Task<IResult> GetAllAuthors(IRepository repository)
{
var students = await repository.GetAllAuthor();
var booksDTO = students.Select(s => s.ToAuthorDto());
return Results.Ok(booksDTO);
}


[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public static async Task<IResult> GetAuthor(IRepository repository, [FromQuery] int id)
{
var author = await repository.GetAuthor(id);
if (author == null)
{
return Results.NotFound("No Authors of the provided Id were found.");
}

var productDTO = new AuthorListDTO
{
Id = author.Id,
FirstName = author.FirstName,
LastName = author.LastName,
Email = author.Email,
Books = author.Books?.Select(s => new BookListDTO
{
Id = s.Id,
Title = s.Title,
//ProfileId = s.ProfileId
}).ToList()
};


return Results.Ok(productDTO);
}


//[ProducesResponseType(StatusCodes.Status201Created)]
//[ProducesResponseType(StatusCodes.Status400BadRequest)]
//public static async Task<IResult> AddBook(IRepository repository, AddBookDTO model)
//{
// try
// {

// Book book = new Book()
// {
// Title = model.Title,
// AuthorId = model.AuthorId
// };
// await repository.CreateAsync(book);

// return TypedResults.Created($"https://localhost:7010/books/{book.Id}", book);


// }
// catch (Exception ex)
// {
// return TypedResults.Problem(ex.Message);
// }
//}
////public static async Task<IResult> Create(IRepository repository, [FromBody] AddBookDTO bookDto)
////{
//// var model = bookDto.ToSkillFromCreateDto();
//// await repository.CreateAsync(model);

//// return Results.CreatedAtRoute(nameof(GetBook), new { id = model.Id }, model.ToSkillDto());

////}
//[ProducesResponseType(StatusCodes.Status200OK)]
//[ProducesResponseType(StatusCodes.Status400BadRequest)]
//[ProducesResponseType(StatusCodes.Status404NotFound)]
//public static async Task<IResult> DeleteBook(IRepository repository, int id)
//{
// try
// {
// var deletedBook = await repository.DeleteAsync(id);
// if (deletedBook != null) return Results.Ok(new { When = DateTime.Now, Status = "Deleted", deletedBook.Title });
// return TypedResults.NotFound();
// }
// catch (Exception ex)
// {
// return TypedResults.Problem(ex.Message);
// }
//}
//[ProducesResponseType(StatusCodes.Status200OK)]
//[ProducesResponseType(StatusCodes.Status400BadRequest)]
//[ProducesResponseType(StatusCodes.Status404NotFound)]
//public static async Task<IResult> UpdateBook(IRepository repository, int id, AddBookDTO model)
//{
// try
// {
// var target = await repository.GetBook(id);
// if (target == null) return Results.NotFound("Product not found");
// if (model.Title != null) target.Title = model.Title; else return Results.BadRequest("error");
// await repository.UpdateBook(target);
// return Results.Ok(target);
// }
// catch (Exception ex)
// {
// return TypedResults.Problem(ex.Message);
// }
//}

}
}
120 changes: 113 additions & 7 deletions exercise.webapi/Endpoints/BookApi.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,126 @@
using exercise.webapi.Models;
using exercise.webapi.DTO;
using exercise.webapi.Mapper;
using exercise.webapi.Models;
using exercise.webapi.Repository;
using static System.Reflection.Metadata.BlobBuilder;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;

namespace exercise.webapi.Endpoints
{
public static class BookApi
{
public static void ConfigureBooksApi(this WebApplication app)
public static void ConfigureBookEndpoint(this WebApplication app)
{
app.MapGet("/books", GetBooks);
var pets = app.MapGroup("books");

pets.MapGet("/getId{id}", GetBook);
pets.MapGet("/getAll", GetAllBooks);
pets.MapPost("/add", AddBook);
pets.MapDelete("/{id}", DeleteBook);
pets.MapPut("/{id}", UpdateBook);
}

[ProducesResponseType(StatusCodes.Status200OK)]
public static async Task<IResult> GetAllBooks(IRepository repository)
{
var students = await repository.GetAllBooks();
var booksDTO = students.Select(s => s.ToBookDto());
return Results.Ok(booksDTO);
}


[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public static async Task<IResult> GetBook(IRepository repository, [FromQuery] int id)
{
var book = await repository.GetBook(id);
if (book == null)
{
return Results.NotFound("No Books of the provided Id were found.");
}

var productDTO = new BookDTO
{
Id = book.Id,
Title = book.Title,
AuthorId = book.AuthorId,
Author = new AuthorDTO
{
Id = book.Author.Id,
FirstName = book.Author.FirstName,
LastName = book.Author.LastName,
Email = book.Author.Email
}
};


return Results.Ok(productDTO);
}


[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public static async Task<IResult> AddBook(IRepository repository, AddBookDTO model)
{
try
{
Book book = new Book()
{
Title = model.Title,
AuthorId = model.AuthorId
};
await repository.CreateAsync(book);

return TypedResults.Created($"https://localhost:7010/books/{book.Id}", book);
}
catch (Exception ex)
{
return TypedResults.Problem(ex.Message);
}
}
//public static async Task<IResult> Create(IRepository repository, [FromBody] AddBookDTO bookDto)
//{
// var model = bookDto.ToSkillFromCreateDto();
// await repository.CreateAsync(model);

// return Results.CreatedAtRoute(nameof(GetBook), new { id = model.Id }, model.ToSkillDto());

private static async Task<IResult> GetBooks(IBookRepository bookRepository)
//}
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public static async Task<IResult> DeleteBook(IRepository repository, int id)
{
var books = await bookRepository.GetAllBooks();
return TypedResults.Ok(books);
try
{
var deletedBook = await repository.DeleteAsync(id);
if (deletedBook != null) return Results.Ok(new { When = DateTime.Now, Status = "Deleted", deletedBook.Title });
return TypedResults.NotFound();
}
catch (Exception ex)
{
return TypedResults.Problem(ex.Message);
}
}
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public static async Task<IResult> UpdateBook(IRepository repository, int id, AddBookDTO model)
{
try
{
var target = await repository.GetBook(id);
if (target == null) return Results.NotFound("Product not found");
if (model.Title != null) target.Title = model.Title; else return Results.BadRequest("error");
await repository.UpdateBook(target);
return Results.Ok(target);
}
catch (Exception ex)
{
return TypedResults.Problem(ex.Message);
}
}

}
}
37 changes: 37 additions & 0 deletions exercise.webapi/Mapper/MappAuthor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using exercise.webapi.DTO;
using exercise.webapi.Models;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace exercise.webapi.Mapper
{
public static class MappAuthor
{
public static AuthorDTO ToProfileNoListsDto(this Author model)
{
return new AuthorDTO
{
Id = model.Id,
FirstName = model.FirstName,
LastName = model.LastName,
Email = model.Email,
};
}

public static AuthorListDTO ToAuthorDto(this Author profileModel)
{
return new AuthorListDTO
{
Id = profileModel.Id,
FirstName = profileModel.FirstName,
LastName = profileModel.LastName,
Email = profileModel.Email,
Books = profileModel.Books?.Select(s => new BookListDTO
{
Id = s.Id,
Title = s.Title,
//ProfileId = s.ProfileId
}).ToList()
};
}
}
}
Loading