Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Cross Platform Apps with Angular and ASP.NET Core

Cross Platform Apps with Angular and ASP.NET Core

Angular provides web developers a platform where applications can be written not only for the web. Mobile devices such as smartphones or tablets, as well as the desktop also consume line of business (LOB) apps and are indispensable. Once the angular code has been written, it doesn't take much to port the application to the smartphone or desktop. In this talk, Fabian Gosebrink shows how an existing angular application can be ported to a mobile device with a few steps using Capacitor and how native features can also be used. With Electron, the desktop is also used as a platform, so that at the end a code base can serve all platforms and the application can be used via any end device.

Fabian Gosebrink

December 03, 2022
Tweet

More Decks by Fabian Gosebrink

Other Decks in Technology

Transcript

  1. namespace DoggoApi.Controllers { [ApiController] [Route("api/[controller]")] [Authorize("access:api")] public class DoggosController :

    ControllerBase { [HttpGet(Name = nameof(GetAllDoggos))] [AllowAnonymous] public ActionResult GetAllDoggos() { List<DoggoEntity> doggos = _repository.GetAll().ToList(); return Ok(_mapper.Map<DoggoDto[]>(doggos)); } [HttpGet] [Route("{id:guid}", Name = nameof(GetSingleDoggo))] public ActionResult GetSingleDoggo(Guid id) { DoggoEntity? entity = _repository.GetSingle(id); if (entity == null) { return NotFound(); } DoggoDto item = _mapper.Map<DoggoDto>(entity); return Ok(item); } [HttpPost(Name = nameof(AddDoggo))] public async Task<ActionResult<DoggoDto>> AddDoggo( [FromBody] DoggoCreateDto createDto) { if (createDto == null) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
  2. namespace DoggoApi.Controllers { [ApiController] [Route("api/[controller]")] [Authorize("access:api")] public class DoggosController :

    ControllerBase { [HttpGet(Name = nameof(GetAllDoggos))] [AllowAnonymous] public ActionResult GetAllDoggos() { List<DoggoEntity> doggos = _repository.GetAll().ToList(); return Ok(_mapper.Map<DoggoDto[]>(doggos)); } [HttpGet] [Route("{id:guid}", Name = nameof(GetSingleDoggo))] public ActionResult GetSingleDoggo(Guid id) { DoggoEntity? entity = _repository.GetSingle(id); if (entity == null) { return NotFound(); } DoggoDto item = _mapper.Map<DoggoDto>(entity); return Ok(item); } [HttpPost(Name = nameof(AddDoggo))] public async Task<ActionResult<DoggoDto>> AddDoggo( [FromBody] DoggoCreateDto createDto) { if (createDto == null) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 [Authorize("access:api")] namespace DoggoApi.Controllers 1 { 2 [ApiController] 3 [Route("api/[controller]")] 4 5 public class DoggosController : ControllerBase 6 { 7 [HttpGet(Name = nameof(GetAllDoggos))] 8 [AllowAnonymous] 9 public ActionResult GetAllDoggos() 10 { 11 List<DoggoEntity> doggos = _repository.GetAll().ToList(); 12 13 return Ok(_mapper.Map<DoggoDto[]>(doggos)); 14 } 15 16 [HttpGet] 17 [Route("{id:guid}", Name = nameof(GetSingleDoggo))] 18 public ActionResult GetSingleDoggo(Guid id) 19 { 20 DoggoEntity? entity = _repository.GetSingle(id); 21 22 if (entity == null) 23 { 24 return NotFound(); 25 } 26 27 DoggoDto item = _mapper.Map<DoggoDto>(entity); 28 29 return Ok(item); 30 } 31 32 [HttpPost(Name = nameof(AddDoggo))] 33 public async Task<ActionResult<DoggoDto>> AddDoggo( 34 [FromBody] DoggoCreateDto createDto) 35 { 36 if (createDto == null) 37
  3. namespace DoggoApi.Controllers { [ApiController] [Route("api/[controller]")] [Authorize("access:api")] public class DoggosController :

    ControllerBase { [HttpGet(Name = nameof(GetAllDoggos))] [AllowAnonymous] public ActionResult GetAllDoggos() { List<DoggoEntity> doggos = _repository.GetAll().ToList(); return Ok(_mapper.Map<DoggoDto[]>(doggos)); } [HttpGet] [Route("{id:guid}", Name = nameof(GetSingleDoggo))] public ActionResult GetSingleDoggo(Guid id) { DoggoEntity? entity = _repository.GetSingle(id); if (entity == null) { return NotFound(); } DoggoDto item = _mapper.Map<DoggoDto>(entity); return Ok(item); } [HttpPost(Name = nameof(AddDoggo))] public async Task<ActionResult<DoggoDto>> AddDoggo( [FromBody] DoggoCreateDto createDto) { if (createDto == null) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 [Authorize("access:api")] namespace DoggoApi.Controllers 1 { 2 [ApiController] 3 [Route("api/[controller]")] 4 5 public class DoggosController : ControllerBase 6 { 7 [HttpGet(Name = nameof(GetAllDoggos))] 8 [AllowAnonymous] 9 public ActionResult GetAllDoggos() 10 { 11 List<DoggoEntity> doggos = _repository.GetAll().ToList(); 12 13 return Ok(_mapper.Map<DoggoDto[]>(doggos)); 14 } 15 16 [HttpGet] 17 [Route("{id:guid}", Name = nameof(GetSingleDoggo))] 18 public ActionResult GetSingleDoggo(Guid id) 19 { 20 DoggoEntity? entity = _repository.GetSingle(id); 21 22 if (entity == null) 23 { 24 return NotFound(); 25 } 26 27 DoggoDto item = _mapper.Map<DoggoDto>(entity); 28 29 return Ok(item); 30 } 31 32 [HttpPost(Name = nameof(AddDoggo))] 33 public async Task<ActionResult<DoggoDto>> AddDoggo( 34 [FromBody] DoggoCreateDto createDto) 35 { 36 if (createDto == null) 37 [HttpGet(Name = nameof(GetAllDoggos))] [AllowAnonymous] public ActionResult GetAllDoggos() { List<DoggoEntity> doggos = _repository.GetAll().ToList(); return Ok(_mapper.Map<DoggoDto[]>(doggos)); } namespace DoggoApi.Controllers 1 { 2 [ApiController] 3 [Route("api/[controller]")] 4 [Authorize("access:api")] 5 public class DoggosController : ControllerBase 6 { 7 8 9 10 11 12 13 14 15 16 [HttpGet] 17 [Route("{id:guid}", Name = nameof(GetSingleDoggo))] 18 public ActionResult GetSingleDoggo(Guid id) 19 { 20 DoggoEntity? entity = _repository.GetSingle(id); 21 22 if (entity == null) 23 { 24 return NotFound(); 25 } 26 27 DoggoDto item = _mapper.Map<DoggoDto>(entity); 28 29 return Ok(item); 30 } 31 32 [HttpPost(Name = nameof(AddDoggo))] 33 public async Task<ActionResult<DoggoDto>> AddDoggo( 34 [FromBody] DoggoCreateDto createDto) 35 { 36 if (createDto == null) 37
  4. namespace DoggoApi.Controllers { [ApiController] [Route("api/[controller]")] [Authorize("access:api")] public class DoggosController :

    ControllerBase { [HttpGet(Name = nameof(GetAllDoggos))] [AllowAnonymous] public ActionResult GetAllDoggos() { List<DoggoEntity> doggos = _repository.GetAll().ToList(); return Ok(_mapper.Map<DoggoDto[]>(doggos)); } [HttpGet] [Route("{id:guid}", Name = nameof(GetSingleDoggo))] public ActionResult GetSingleDoggo(Guid id) { DoggoEntity? entity = _repository.GetSingle(id); if (entity == null) { return NotFound(); } DoggoDto item = _mapper.Map<DoggoDto>(entity); return Ok(item); } [HttpPost(Name = nameof(AddDoggo))] public async Task<ActionResult<DoggoDto>> AddDoggo( [FromBody] DoggoCreateDto createDto) { if (createDto == null) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 [Authorize("access:api")] namespace DoggoApi.Controllers 1 { 2 [ApiController] 3 [Route("api/[controller]")] 4 5 public class DoggosController : ControllerBase 6 { 7 [HttpGet(Name = nameof(GetAllDoggos))] 8 [AllowAnonymous] 9 public ActionResult GetAllDoggos() 10 { 11 List<DoggoEntity> doggos = _repository.GetAll().ToList(); 12 13 return Ok(_mapper.Map<DoggoDto[]>(doggos)); 14 } 15 16 [HttpGet] 17 [Route("{id:guid}", Name = nameof(GetSingleDoggo))] 18 public ActionResult GetSingleDoggo(Guid id) 19 { 20 DoggoEntity? entity = _repository.GetSingle(id); 21 22 if (entity == null) 23 { 24 return NotFound(); 25 } 26 27 DoggoDto item = _mapper.Map<DoggoDto>(entity); 28 29 return Ok(item); 30 } 31 32 [HttpPost(Name = nameof(AddDoggo))] 33 public async Task<ActionResult<DoggoDto>> AddDoggo( 34 [FromBody] DoggoCreateDto createDto) 35 { 36 if (createDto == null) 37 [HttpGet(Name = nameof(GetAllDoggos))] [AllowAnonymous] public ActionResult GetAllDoggos() { List<DoggoEntity> doggos = _repository.GetAll().ToList(); return Ok(_mapper.Map<DoggoDto[]>(doggos)); } namespace DoggoApi.Controllers 1 { 2 [ApiController] 3 [Route("api/[controller]")] 4 [Authorize("access:api")] 5 public class DoggosController : ControllerBase 6 { 7 8 9 10 11 12 13 14 15 16 [HttpGet] 17 [Route("{id:guid}", Name = nameof(GetSingleDoggo))] 18 public ActionResult GetSingleDoggo(Guid id) 19 { 20 DoggoEntity? entity = _repository.GetSingle(id); 21 22 if (entity == null) 23 { 24 return NotFound(); 25 } 26 27 DoggoDto item = _mapper.Map<DoggoDto>(entity); 28 29 return Ok(item); 30 } 31 32 [HttpPost(Name = nameof(AddDoggo))] 33 public async Task<ActionResult<DoggoDto>> AddDoggo( 34 [FromBody] DoggoCreateDto createDto) 35 { 36 if (createDto == null) 37 [HttpGet] [Route("{id:guid}", Name = nameof(GetSingleDoggo))] public ActionResult GetSingleDoggo(Guid id) { DoggoEntity? entity = _repository.GetSingle(id); if (entity == null) { return NotFound(); } DoggoDto item = _mapper.Map<DoggoDto>(entity); return Ok(item); } [Authorize( access:api )] 5 public class DoggosController : ControllerBase 6 { 7 [HttpGet(Name = nameof(GetAllDoggos))] 8 [AllowAnonymous] 9 public ActionResult GetAllDoggos() 10 { 11 List<DoggoEntity> doggos = _repository.GetAll().ToList(); 12 13 return Ok(_mapper.Map<DoggoDto[]>(doggos)); 14 } 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 [HttpPost(Name = nameof(AddDoggo))] 33 public async Task<ActionResult<DoggoDto>> AddDoggo( 34 [FromBody] DoggoCreateDto createDto) 35 { 36 if (createDto == null) 37 { 38 return BadRequest(); 39 } 40 41 /* ... */ 42
  5. namespace DoggoApi.Controllers { [ApiController] [Route("api/[controller]")] [Authorize("access:api")] public class DoggosController :

    ControllerBase { [HttpGet(Name = nameof(GetAllDoggos))] [AllowAnonymous] public ActionResult GetAllDoggos() { List<DoggoEntity> doggos = _repository.GetAll().ToList(); return Ok(_mapper.Map<DoggoDto[]>(doggos)); } [HttpGet] [Route("{id:guid}", Name = nameof(GetSingleDoggo))] public ActionResult GetSingleDoggo(Guid id) { DoggoEntity? entity = _repository.GetSingle(id); if (entity == null) { return NotFound(); } DoggoDto item = _mapper.Map<DoggoDto>(entity); return Ok(item); } [HttpPost(Name = nameof(AddDoggo))] public async Task<ActionResult<DoggoDto>> AddDoggo( [FromBody] DoggoCreateDto createDto) { if (createDto == null) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 [Authorize("access:api")] namespace DoggoApi.Controllers 1 { 2 [ApiController] 3 [Route("api/[controller]")] 4 5 public class DoggosController : ControllerBase 6 { 7 [HttpGet(Name = nameof(GetAllDoggos))] 8 [AllowAnonymous] 9 public ActionResult GetAllDoggos() 10 { 11 List<DoggoEntity> doggos = _repository.GetAll().ToList(); 12 13 return Ok(_mapper.Map<DoggoDto[]>(doggos)); 14 } 15 16 [HttpGet] 17 [Route("{id:guid}", Name = nameof(GetSingleDoggo))] 18 public ActionResult GetSingleDoggo(Guid id) 19 { 20 DoggoEntity? entity = _repository.GetSingle(id); 21 22 if (entity == null) 23 { 24 return NotFound(); 25 } 26 27 DoggoDto item = _mapper.Map<DoggoDto>(entity); 28 29 return Ok(item); 30 } 31 32 [HttpPost(Name = nameof(AddDoggo))] 33 public async Task<ActionResult<DoggoDto>> AddDoggo( 34 [FromBody] DoggoCreateDto createDto) 35 { 36 if (createDto == null) 37 [HttpGet(Name = nameof(GetAllDoggos))] [AllowAnonymous] public ActionResult GetAllDoggos() { List<DoggoEntity> doggos = _repository.GetAll().ToList(); return Ok(_mapper.Map<DoggoDto[]>(doggos)); } namespace DoggoApi.Controllers 1 { 2 [ApiController] 3 [Route("api/[controller]")] 4 [Authorize("access:api")] 5 public class DoggosController : ControllerBase 6 { 7 8 9 10 11 12 13 14 15 16 [HttpGet] 17 [Route("{id:guid}", Name = nameof(GetSingleDoggo))] 18 public ActionResult GetSingleDoggo(Guid id) 19 { 20 DoggoEntity? entity = _repository.GetSingle(id); 21 22 if (entity == null) 23 { 24 return NotFound(); 25 } 26 27 DoggoDto item = _mapper.Map<DoggoDto>(entity); 28 29 return Ok(item); 30 } 31 32 [HttpPost(Name = nameof(AddDoggo))] 33 public async Task<ActionResult<DoggoDto>> AddDoggo( 34 [FromBody] DoggoCreateDto createDto) 35 { 36 if (createDto == null) 37 [HttpGet] [Route("{id:guid}", Name = nameof(GetSingleDoggo))] public ActionResult GetSingleDoggo(Guid id) { DoggoEntity? entity = _repository.GetSingle(id); if (entity == null) { return NotFound(); } DoggoDto item = _mapper.Map<DoggoDto>(entity); return Ok(item); } namespace DoggoApi.Controllers 1 { 2 [ApiController] 3 [Route("api/[controller]")] 4 [Authorize("access:api")] 5 public class DoggosController : ControllerBase 6 { 7 [HttpGet(Name = nameof(GetAllDoggos))] 8 [AllowAnonymous] 9 public ActionResult GetAllDoggos() 10 { 11 List<DoggoEntity> doggos = _repository.GetAll().ToList(); 12 13 return Ok(_mapper.Map<DoggoDto[]>(doggos)); 14 } 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 [HttpPost(Name = nameof(AddDoggo))] 33 public async Task<ActionResult<DoggoDto>> AddDoggo( 34 [FromBody] DoggoCreateDto createDto) 35 { 36 if (createDto == null) 37 [HttpPost(Name = nameof(AddDoggo))] public async Task<ActionResult<DoggoDto>> AddDoggo( [FromBody] DoggoCreateDto createDto) { if (createDto == null) { return BadRequest(); } /* ... */ await _hubContext.Clients.All.SendAsync("DoggoAdded", dto); return CreatedAtRoute(nameof(GetSingleDoggo), new { id = newItem?.Id }, dto); } 22 if (entity == null) 23 { 24 return NotFound(); 25 } 26 27 DoggoDto item = _mapper.Map<DoggoDto>(entity); 28 29 return Ok(item); 30 } 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 [HttpPut] 50 [Route("rate/{id:guid}", Name = nameof(RateDoggo))] 51 [AllowAnonymous] 52 public async Task<ActionResult<DoggoDto>> RateDoggo( 53 Guid id, [FromBody] DoggoRateDto rateDto) 54 { 55 if (rateDto == null) 56 { 57 return BadRequest(); 58
  6. namespace DoggoApi.Controllers { [ApiController] [Route("api/[controller]")] [Authorize("access:api")] public class DoggosController :

    ControllerBase { [HttpGet(Name = nameof(GetAllDoggos))] [AllowAnonymous] public ActionResult GetAllDoggos() { List<DoggoEntity> doggos = _repository.GetAll().ToList(); return Ok(_mapper.Map<DoggoDto[]>(doggos)); } [HttpGet] [Route("{id:guid}", Name = nameof(GetSingleDoggo))] public ActionResult GetSingleDoggo(Guid id) { DoggoEntity? entity = _repository.GetSingle(id); if (entity == null) { return NotFound(); } DoggoDto item = _mapper.Map<DoggoDto>(entity); return Ok(item); } [HttpPost(Name = nameof(AddDoggo))] public async Task<ActionResult<DoggoDto>> AddDoggo( [FromBody] DoggoCreateDto createDto) { if (createDto == null) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 [Authorize("access:api")] namespace DoggoApi.Controllers 1 { 2 [ApiController] 3 [Route("api/[controller]")] 4 5 public class DoggosController : ControllerBase 6 { 7 [HttpGet(Name = nameof(GetAllDoggos))] 8 [AllowAnonymous] 9 public ActionResult GetAllDoggos() 10 { 11 List<DoggoEntity> doggos = _repository.GetAll().ToList(); 12 13 return Ok(_mapper.Map<DoggoDto[]>(doggos)); 14 } 15 16 [HttpGet] 17 [Route("{id:guid}", Name = nameof(GetSingleDoggo))] 18 public ActionResult GetSingleDoggo(Guid id) 19 { 20 DoggoEntity? entity = _repository.GetSingle(id); 21 22 if (entity == null) 23 { 24 return NotFound(); 25 } 26 27 DoggoDto item = _mapper.Map<DoggoDto>(entity); 28 29 return Ok(item); 30 } 31 32 [HttpPost(Name = nameof(AddDoggo))] 33 public async Task<ActionResult<DoggoDto>> AddDoggo( 34 [FromBody] DoggoCreateDto createDto) 35 { 36 if (createDto == null) 37 [HttpGet(Name = nameof(GetAllDoggos))] [AllowAnonymous] public ActionResult GetAllDoggos() { List<DoggoEntity> doggos = _repository.GetAll().ToList(); return Ok(_mapper.Map<DoggoDto[]>(doggos)); } namespace DoggoApi.Controllers 1 { 2 [ApiController] 3 [Route("api/[controller]")] 4 [Authorize("access:api")] 5 public class DoggosController : ControllerBase 6 { 7 8 9 10 11 12 13 14 15 16 [HttpGet] 17 [Route("{id:guid}", Name = nameof(GetSingleDoggo))] 18 public ActionResult GetSingleDoggo(Guid id) 19 { 20 DoggoEntity? entity = _repository.GetSingle(id); 21 22 if (entity == null) 23 { 24 return NotFound(); 25 } 26 27 DoggoDto item = _mapper.Map<DoggoDto>(entity); 28 29 return Ok(item); 30 } 31 32 [HttpPost(Name = nameof(AddDoggo))] 33 public async Task<ActionResult<DoggoDto>> AddDoggo( 34 [FromBody] DoggoCreateDto createDto) 35 { 36 if (createDto == null) 37 [HttpGet] [Route("{id:guid}", Name = nameof(GetSingleDoggo))] public ActionResult GetSingleDoggo(Guid id) { DoggoEntity? entity = _repository.GetSingle(id); if (entity == null) { return NotFound(); } DoggoDto item = _mapper.Map<DoggoDto>(entity); return Ok(item); } namespace DoggoApi.Controllers 1 { 2 [ApiController] 3 [Route("api/[controller]")] 4 [Authorize("access:api")] 5 public class DoggosController : ControllerBase 6 { 7 [HttpGet(Name = nameof(GetAllDoggos))] 8 [AllowAnonymous] 9 public ActionResult GetAllDoggos() 10 { 11 List<DoggoEntity> doggos = _repository.GetAll().ToList(); 12 13 return Ok(_mapper.Map<DoggoDto[]>(doggos)); 14 } 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 [HttpPost(Name = nameof(AddDoggo))] 33 public async Task<ActionResult<DoggoDto>> AddDoggo( 34 [FromBody] DoggoCreateDto createDto) 35 { 36 if (createDto == null) 37 [HttpPost(Name = nameof(AddDoggo))] public async Task<ActionResult<DoggoDto>> AddDoggo( [FromBody] DoggoCreateDto createDto) { if (createDto == null) namespace DoggoApi.Controllers 1 { 2 [ApiController] 3 [Route("api/[controller]")] 4 [Authorize("access:api")] 5 public class DoggosController : ControllerBase 6 { 7 [HttpGet(Name = nameof(GetAllDoggos))] 8 [AllowAnonymous] 9 public ActionResult GetAllDoggos() 10 { 11 List<DoggoEntity> doggos = _repository.GetAll().ToList(); 12 13 return Ok(_mapper.Map<DoggoDto[]>(doggos)); 14 } 15 16 [HttpGet] 17 [Route("{id:guid}", Name = nameof(GetSingleDoggo))] 18 public ActionResult GetSingleDoggo(Guid id) 19 { 20 DoggoEntity? entity = _repository.GetSingle(id); 21 22 if (entity == null) 23 { 24 return NotFound(); 25 } 26 27 DoggoDto item = _mapper.Map<DoggoDto>(entity); 28 29 return Ok(item); 30 } 31 32 33 34 35 36 37 await _hubContext.Clients.All.SendAsync("DoggoAdded", dto); return NotFound(); 25 } 26 27 DoggoDto item = _mapper.Map<DoggoDto>(entity); 28 29 return Ok(item); 30 } 31 32 [HttpPost(Name = nameof(AddDoggo))] 33 public async Task<ActionResult<DoggoDto>> AddDoggo( 34 [FromBody] DoggoCreateDto createDto) 35 { 36 if (createDto == null) 37 { 38 return BadRequest(); 39 } 40 41 /* ... */ 42 43 44 45 return CreatedAtRoute(nameof(GetSingleDoggo), 46 new { id = newItem?.Id }, dto); 47 } 48 49 [HttpPut] 50 [Route("rate/{id:guid}", Name = nameof(RateDoggo))] 51 [AllowAnonymous] 52 public async Task<ActionResult<DoggoDto>> RateDoggo( 53 Guid id, [FromBody] DoggoRateDto rateDto) 54 { 55 if (rateDto == null) 56 { 57 return BadRequest(); 58 } 59 60 DoggoEntity? existingEntity = _repository.GetSingle(id); 61 62
  7. namespace DoggoApi.Controllers { [ApiController] [Route("api/[controller]")] [Authorize("access:api")] public class DoggosController :

    ControllerBase { [HttpGet(Name = nameof(GetAllDoggos))] [AllowAnonymous] public ActionResult GetAllDoggos() { List<DoggoEntity> doggos = _repository.GetAll().ToList(); return Ok(_mapper.Map<DoggoDto[]>(doggos)); } [HttpGet] [Route("{id:guid}", Name = nameof(GetSingleDoggo))] public ActionResult GetSingleDoggo(Guid id) { DoggoEntity? entity = _repository.GetSingle(id); if (entity == null) { return NotFound(); } DoggoDto item = _mapper.Map<DoggoDto>(entity); return Ok(item); } [HttpPost(Name = nameof(AddDoggo))] public async Task<ActionResult<DoggoDto>> AddDoggo( [FromBody] DoggoCreateDto createDto) { if (createDto == null) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 [Authorize("access:api")] namespace DoggoApi.Controllers 1 { 2 [ApiController] 3 [Route("api/[controller]")] 4 5 public class DoggosController : ControllerBase 6 { 7 [HttpGet(Name = nameof(GetAllDoggos))] 8 [AllowAnonymous] 9 public ActionResult GetAllDoggos() 10 { 11 List<DoggoEntity> doggos = _repository.GetAll().ToList(); 12 13 return Ok(_mapper.Map<DoggoDto[]>(doggos)); 14 } 15 16 [HttpGet] 17 [Route("{id:guid}", Name = nameof(GetSingleDoggo))] 18 public ActionResult GetSingleDoggo(Guid id) 19 { 20 DoggoEntity? entity = _repository.GetSingle(id); 21 22 if (entity == null) 23 { 24 return NotFound(); 25 } 26 27 DoggoDto item = _mapper.Map<DoggoDto>(entity); 28 29 return Ok(item); 30 } 31 32 [HttpPost(Name = nameof(AddDoggo))] 33 public async Task<ActionResult<DoggoDto>> AddDoggo( 34 [FromBody] DoggoCreateDto createDto) 35 { 36 if (createDto == null) 37 [HttpGet(Name = nameof(GetAllDoggos))] [AllowAnonymous] public ActionResult GetAllDoggos() { List<DoggoEntity> doggos = _repository.GetAll().ToList(); return Ok(_mapper.Map<DoggoDto[]>(doggos)); } namespace DoggoApi.Controllers 1 { 2 [ApiController] 3 [Route("api/[controller]")] 4 [Authorize("access:api")] 5 public class DoggosController : ControllerBase 6 { 7 8 9 10 11 12 13 14 15 16 [HttpGet] 17 [Route("{id:guid}", Name = nameof(GetSingleDoggo))] 18 public ActionResult GetSingleDoggo(Guid id) 19 { 20 DoggoEntity? entity = _repository.GetSingle(id); 21 22 if (entity == null) 23 { 24 return NotFound(); 25 } 26 27 DoggoDto item = _mapper.Map<DoggoDto>(entity); 28 29 return Ok(item); 30 } 31 32 [HttpPost(Name = nameof(AddDoggo))] 33 public async Task<ActionResult<DoggoDto>> AddDoggo( 34 [FromBody] DoggoCreateDto createDto) 35 { 36 if (createDto == null) 37 [HttpGet] [Route("{id:guid}", Name = nameof(GetSingleDoggo))] public ActionResult GetSingleDoggo(Guid id) { DoggoEntity? entity = _repository.GetSingle(id); if (entity == null) { return NotFound(); } DoggoDto item = _mapper.Map<DoggoDto>(entity); return Ok(item); } namespace DoggoApi.Controllers 1 { 2 [ApiController] 3 [Route("api/[controller]")] 4 [Authorize("access:api")] 5 public class DoggosController : ControllerBase 6 { 7 [HttpGet(Name = nameof(GetAllDoggos))] 8 [AllowAnonymous] 9 public ActionResult GetAllDoggos() 10 { 11 List<DoggoEntity> doggos = _repository.GetAll().ToList(); 12 13 return Ok(_mapper.Map<DoggoDto[]>(doggos)); 14 } 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 [HttpPost(Name = nameof(AddDoggo))] 33 public async Task<ActionResult<DoggoDto>> AddDoggo( 34 [FromBody] DoggoCreateDto createDto) 35 { 36 if (createDto == null) 37 [HttpPost(Name = nameof(AddDoggo))] public async Task<ActionResult<DoggoDto>> AddDoggo( [FromBody] DoggoCreateDto createDto) { if (createDto == null) namespace DoggoApi.Controllers 1 { 2 [ApiController] 3 [Route("api/[controller]")] 4 [Authorize("access:api")] 5 public class DoggosController : ControllerBase 6 { 7 [HttpGet(Name = nameof(GetAllDoggos))] 8 [AllowAnonymous] 9 public ActionResult GetAllDoggos() 10 { 11 List<DoggoEntity> doggos = _repository.GetAll().ToList(); 12 13 return Ok(_mapper.Map<DoggoDto[]>(doggos)); 14 } 15 16 [HttpGet] 17 [Route("{id:guid}", Name = nameof(GetSingleDoggo))] 18 public ActionResult GetSingleDoggo(Guid id) 19 { 20 DoggoEntity? entity = _repository.GetSingle(id); 21 22 if (entity == null) 23 { 24 return NotFound(); 25 } 26 27 DoggoDto item = _mapper.Map<DoggoDto>(entity); 28 29 return Ok(item); 30 } 31 32 33 34 35 36 37 namespace DoggoApi.Controllers 1 { 2 [ApiController] 3 [Route("api/[controller]")] 4 [Authorize("access:api")] 5 public class DoggosController : ControllerBase 6 { 7 [HttpGet(Name = nameof(GetAllDoggos))] 8 [AllowAnonymous] 9 public ActionResult GetAllDoggos() 10 { 11 List<DoggoEntity> doggos = _repository.GetAll().ToList(); 12 13 return Ok(_mapper.Map<DoggoDto[]>(doggos)); 14 } 15 16 [HttpGet] 17 [Route("{id:guid}", Name = nameof(GetSingleDoggo))] 18 public ActionResult GetSingleDoggo(Guid id) 19 { 20 DoggoEntity? entity = _repository.GetSingle(id); 21 22 if (entity == null) 23 { 24 return NotFound(); 25 } 26 27 DoggoDto item = _mapper.Map<DoggoDto>(entity); 28 29 return Ok(item); 30 } 31 32 [HttpPost(Name = nameof(AddDoggo))] 33 public async Task<ActionResult<DoggoDto>> AddDoggo( 34 [FromBody] DoggoCreateDto createDto) 35 { 36 if (createDto == null) 37 [HttpPut] [Route("rate/{id:guid}", Name = nameof(RateDoggo))] [AllowAnonymous] public async Task<ActionResult<DoggoDto>> RateDoggo( Guid id, [FromBody] DoggoRateDto rateDto) { if (rateDto == null) { return BadRequest(); } DoggoEntity? existingEntity = _repository.GetSingle(id); existingEntity.RatingCount = existingEntity.RatingCount + 1; existingEntity.RatingSum = existingEntity.RatingSum + rateDto.Value; _repository.Update(existingEntity); if (!_repository.Save()) { throw new Exception("Updating an item failed on save."); } DoggoDto dto = _mapper.Map<DoggoDto>(existingEntity); await _hubContext.Clients.All.SendAsync("DoggoRated", dto); return Ok(dto); } / ... / 42 43 await _hubContext.Clients.All.SendAsync("DoggoAdded", dto); 44 45 return CreatedAtRoute(nameof(GetSingleDoggo), 46 new { id = newItem?.Id }, dto); 47 } 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 } 79
  8. namespace DoggoApi.Controllers { [ApiController] [Route("api/[controller]")] [Authorize("access:api")] public class DoggosController :

    ControllerBase { [HttpGet(Name = nameof(GetAllDoggos))] [AllowAnonymous] public ActionResult GetAllDoggos() { List<DoggoEntity> doggos = _repository.GetAll().ToList(); return Ok(_mapper.Map<DoggoDto[]>(doggos)); } [HttpGet] [Route("{id:guid}", Name = nameof(GetSingleDoggo))] public ActionResult GetSingleDoggo(Guid id) { DoggoEntity? entity = _repository.GetSingle(id); if (entity == null) { return NotFound(); } DoggoDto item = _mapper.Map<DoggoDto>(entity); return Ok(item); } [HttpPost(Name = nameof(AddDoggo))] public async Task<ActionResult<DoggoDto>> AddDoggo( [FromBody] DoggoCreateDto createDto) { if (createDto == null) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 [Authorize("access:api")] namespace DoggoApi.Controllers 1 { 2 [ApiController] 3 [Route("api/[controller]")] 4 5 public class DoggosController : ControllerBase 6 { 7 [HttpGet(Name = nameof(GetAllDoggos))] 8 [AllowAnonymous] 9 public ActionResult GetAllDoggos() 10 { 11 List<DoggoEntity> doggos = _repository.GetAll().ToList(); 12 13 return Ok(_mapper.Map<DoggoDto[]>(doggos)); 14 } 15 16 [HttpGet] 17 [Route("{id:guid}", Name = nameof(GetSingleDoggo))] 18 public ActionResult GetSingleDoggo(Guid id) 19 { 20 DoggoEntity? entity = _repository.GetSingle(id); 21 22 if (entity == null) 23 { 24 return NotFound(); 25 } 26 27 DoggoDto item = _mapper.Map<DoggoDto>(entity); 28 29 return Ok(item); 30 } 31 32 [HttpPost(Name = nameof(AddDoggo))] 33 public async Task<ActionResult<DoggoDto>> AddDoggo( 34 [FromBody] DoggoCreateDto createDto) 35 { 36 if (createDto == null) 37 [HttpGet(Name = nameof(GetAllDoggos))] [AllowAnonymous] public ActionResult GetAllDoggos() { List<DoggoEntity> doggos = _repository.GetAll().ToList(); return Ok(_mapper.Map<DoggoDto[]>(doggos)); } namespace DoggoApi.Controllers 1 { 2 [ApiController] 3 [Route("api/[controller]")] 4 [Authorize("access:api")] 5 public class DoggosController : ControllerBase 6 { 7 8 9 10 11 12 13 14 15 16 [HttpGet] 17 [Route("{id:guid}", Name = nameof(GetSingleDoggo))] 18 public ActionResult GetSingleDoggo(Guid id) 19 { 20 DoggoEntity? entity = _repository.GetSingle(id); 21 22 if (entity == null) 23 { 24 return NotFound(); 25 } 26 27 DoggoDto item = _mapper.Map<DoggoDto>(entity); 28 29 return Ok(item); 30 } 31 32 [HttpPost(Name = nameof(AddDoggo))] 33 public async Task<ActionResult<DoggoDto>> AddDoggo( 34 [FromBody] DoggoCreateDto createDto) 35 { 36 if (createDto == null) 37 [HttpGet] [Route("{id:guid}", Name = nameof(GetSingleDoggo))] public ActionResult GetSingleDoggo(Guid id) { DoggoEntity? entity = _repository.GetSingle(id); if (entity == null) { return NotFound(); } DoggoDto item = _mapper.Map<DoggoDto>(entity); return Ok(item); } namespace DoggoApi.Controllers 1 { 2 [ApiController] 3 [Route("api/[controller]")] 4 [Authorize("access:api")] 5 public class DoggosController : ControllerBase 6 { 7 [HttpGet(Name = nameof(GetAllDoggos))] 8 [AllowAnonymous] 9 public ActionResult GetAllDoggos() 10 { 11 List<DoggoEntity> doggos = _repository.GetAll().ToList(); 12 13 return Ok(_mapper.Map<DoggoDto[]>(doggos)); 14 } 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 [HttpPost(Name = nameof(AddDoggo))] 33 public async Task<ActionResult<DoggoDto>> AddDoggo( 34 [FromBody] DoggoCreateDto createDto) 35 { 36 if (createDto == null) 37 [HttpPost(Name = nameof(AddDoggo))] public async Task<ActionResult<DoggoDto>> AddDoggo( [FromBody] DoggoCreateDto createDto) { if (createDto == null) namespace DoggoApi.Controllers 1 { 2 [ApiController] 3 [Route("api/[controller]")] 4 [Authorize("access:api")] 5 public class DoggosController : ControllerBase 6 { 7 [HttpGet(Name = nameof(GetAllDoggos))] 8 [AllowAnonymous] 9 public ActionResult GetAllDoggos() 10 { 11 List<DoggoEntity> doggos = _repository.GetAll().ToList(); 12 13 return Ok(_mapper.Map<DoggoDto[]>(doggos)); 14 } 15 16 [HttpGet] 17 [Route("{id:guid}", Name = nameof(GetSingleDoggo))] 18 public ActionResult GetSingleDoggo(Guid id) 19 { 20 DoggoEntity? entity = _repository.GetSingle(id); 21 22 if (entity == null) 23 { 24 return NotFound(); 25 } 26 27 DoggoDto item = _mapper.Map<DoggoDto>(entity); 28 29 return Ok(item); 30 } 31 32 33 34 35 36 37 namespace DoggoApi.Controllers 1 { 2 [ApiController] 3 [Route("api/[controller]")] 4 [Authorize("access:api")] 5 public class DoggosController : ControllerBase 6 { 7 [HttpGet(Name = nameof(GetAllDoggos))] 8 [AllowAnonymous] 9 public ActionResult GetAllDoggos() 10 { 11 List<DoggoEntity> doggos = _repository.GetAll().ToList(); 12 13 return Ok(_mapper.Map<DoggoDto[]>(doggos)); 14 } 15 16 [HttpGet] 17 [Route("{id:guid}", Name = nameof(GetSingleDoggo))] 18 public ActionResult GetSingleDoggo(Guid id) 19 { 20 DoggoEntity? entity = _repository.GetSingle(id); 21 22 if (entity == null) 23 { 24 return NotFound(); 25 } 26 27 DoggoDto item = _mapper.Map<DoggoDto>(entity); 28 29 return Ok(item); 30 } 31 32 [HttpPost(Name = nameof(AddDoggo))] 33 public async Task<ActionResult<DoggoDto>> AddDoggo( 34 [FromBody] DoggoCreateDto createDto) 35 { 36 if (createDto == null) 37 namespace DoggoApi.Controllers 1 { 2 [ApiController] 3 [Route("api/[controller]")] 4 [Authorize("access:api")] 5 public class DoggosController : ControllerBase 6 { 7 [HttpGet(Name = nameof(GetAllDoggos))] 8 [AllowAnonymous] 9 public ActionResult GetAllDoggos() 10 { 11 List<DoggoEntity> doggos = _repository.GetAll().ToList(); 12 13 return Ok(_mapper.Map<DoggoDto[]>(doggos)); 14 } 15 16 [HttpGet] 17 [Route("{id:guid}", Name = nameof(GetSingleDoggo))] 18 public ActionResult GetSingleDoggo(Guid id) 19 { 20 DoggoEntity? entity = _repository.GetSingle(id); 21 22 if (entity == null) 23 { 24 return NotFound(); 25 } 26 27 DoggoDto item = _mapper.Map<DoggoDto>(entity); 28 29 return Ok(item); 30 } 31 32 [HttpPost(Name = nameof(AddDoggo))] 33 public async Task<ActionResult<DoggoDto>> AddDoggo( 34 [FromBody] DoggoCreateDto createDto) 35 { 36 if (createDto == null) 37 await _hubContext.Clients.All.SendAsync("DoggoRated", dto); / ... / 42 43 await _hubContext.Clients.All.SendAsync("DoggoAdded", dto); 44 45 return CreatedAtRoute(nameof(GetSingleDoggo), 46 new { id = newItem?.Id }, dto); 47 } 48 49 [HttpPut] 50 [Route("rate/{id:guid}", Name = nameof(RateDoggo))] 51 [AllowAnonymous] 52 public async Task<ActionResult<DoggoDto>> RateDoggo( 53 Guid id, [FromBody] DoggoRateDto rateDto) 54 { 55 if (rateDto == null) 56 { 57 return BadRequest(); 58 } 59 60 DoggoEntity? existingEntity = _repository.GetSingle(id); 61 62 existingEntity.RatingCount = existingEntity.RatingCount + 1; 63 existingEntity.RatingSum = existingEntity.RatingSum + rateDto.Value; 64 65 _repository.Update(existingEntity); 66 67 if (!_repository.Save()) 68 { 69 throw new Exception("Updating an item failed on save."); 70 } 71 72 DoggoDto dto = _mapper.Map<DoggoDto>(existingEntity); 73 74 75 76 return Ok(dto); 77 } 78 } 79