Added ServiceController
This commit is contained in:
		
							
								
								
									
										103
									
								
								Birdmap.API/Controllers/ServiceController.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										103
									
								
								Birdmap.API/Controllers/ServiceController.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,103 @@
 | 
				
			|||||||
 | 
					using AutoMapper;
 | 
				
			||||||
 | 
					using Birdmap.API.DTOs;
 | 
				
			||||||
 | 
					using Birdmap.BLL.Interfaces;
 | 
				
			||||||
 | 
					using Birdmap.DAL.Entities;
 | 
				
			||||||
 | 
					using Microsoft.AspNetCore.Authorization;
 | 
				
			||||||
 | 
					using Microsoft.AspNetCore.Http;
 | 
				
			||||||
 | 
					using Microsoft.AspNetCore.Mvc;
 | 
				
			||||||
 | 
					using Microsoft.Extensions.Configuration;
 | 
				
			||||||
 | 
					using Microsoft.Extensions.Logging;
 | 
				
			||||||
 | 
					using System;
 | 
				
			||||||
 | 
					using System.Collections.Generic;
 | 
				
			||||||
 | 
					using System.Linq;
 | 
				
			||||||
 | 
					using System.Net.Http;
 | 
				
			||||||
 | 
					using System.Runtime.InteropServices.ComTypes;
 | 
				
			||||||
 | 
					using System.Threading.Tasks;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace Birdmap.API.Controllers
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    [Authorize]
 | 
				
			||||||
 | 
					    [ApiController]
 | 
				
			||||||
 | 
					    [Route("api/[controller]")]
 | 
				
			||||||
 | 
					    public class ServiceController : ControllerBase
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        private readonly IServiceService _service;
 | 
				
			||||||
 | 
					        private readonly IMapper _mapper;
 | 
				
			||||||
 | 
					        private readonly ILogger<ServiceController> _logger;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public ServiceController(IServiceService service, IMapper mapper, ILogger<ServiceController> logger)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            _service = service;
 | 
				
			||||||
 | 
					            _mapper = mapper;
 | 
				
			||||||
 | 
					            _logger = logger;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        [HttpGet]
 | 
				
			||||||
 | 
					        [ProducesResponseType(StatusCodes.Status200OK)]
 | 
				
			||||||
 | 
					        public async Task<ActionResult<List<ServiceInfo>>> GetAsync()
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            _logger.LogInformation($"Getting all services from db...");
 | 
				
			||||||
 | 
					            var serviceInfos = (await _service.GetAllServicesAsync())
 | 
				
			||||||
 | 
					                .Select(s => new ServiceInfo { Service = _mapper.Map<ServiceRequest>(s) });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            var client = new HttpClient();
 | 
				
			||||||
 | 
					            foreach (var si in serviceInfos)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                try
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    _logger.LogInformation($"Sending a request to service '{si.Service.Name}' with url '{si.Service.Uri}'...");
 | 
				
			||||||
 | 
					                    var response = await client.GetAsync(si.Service.Uri);
 | 
				
			||||||
 | 
					                    si.StatusCode = response.StatusCode;
 | 
				
			||||||
 | 
					                    si.Response = await response.Content.ReadAsStringAsync();
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                catch (Exception ex)
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    _logger.LogWarning($"Requesting service '{si.Service.Name}' faulted.");
 | 
				
			||||||
 | 
					                    si.StatusCode = System.Net.HttpStatusCode.ServiceUnavailable;
 | 
				
			||||||
 | 
					                    si.Response = ex.ToString();
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            return serviceInfos.ToList();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        [HttpPost]
 | 
				
			||||||
 | 
					        [ProducesResponseType(StatusCodes.Status201Created)]
 | 
				
			||||||
 | 
					        public async Task<ActionResult<ServiceRequest>> PostAsync(ServiceRequest request)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            _logger.LogInformation($"Creating service {request.Name}...");
 | 
				
			||||||
 | 
					            var created = await _service.CreateServiceAsync(
 | 
				
			||||||
 | 
					                _mapper.Map<Service>(request));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            _logger.LogInformation($"Created service [{created.Id}].");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            return CreatedAtAction(
 | 
				
			||||||
 | 
					                nameof(GetAsync),
 | 
				
			||||||
 | 
					                _mapper.Map<ServiceRequest>(created));
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        [HttpPut]
 | 
				
			||||||
 | 
					        [ProducesResponseType(StatusCodes.Status204NoContent)]
 | 
				
			||||||
 | 
					        public async Task<IActionResult> PutAsync(ServiceRequest request)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            _logger.LogInformation($"Updating service {request.Name}...");
 | 
				
			||||||
 | 
					            var service = _mapper.Map<Service>(request);
 | 
				
			||||||
 | 
					            service.IsFromConfig = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            await _service.UpdateServiceAsync(service);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            return NoContent();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        [HttpDelete("{id}")]
 | 
				
			||||||
 | 
					        [ProducesResponseType(StatusCodes.Status204NoContent)]
 | 
				
			||||||
 | 
					        public async Task<IActionResult> DeleteAsync(int id)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            _logger.LogInformation($"Deleting service [{id}]...");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            await _service.DeleteServiceAsync(id);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            return NoContent();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										11
									
								
								Birdmap.API/DTOs/ServiceInfo.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								Birdmap.API/DTOs/ServiceInfo.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,11 @@
 | 
				
			|||||||
 | 
					using System.Net;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace Birdmap.API.DTOs
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    public class ServiceInfo
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        public ServiceRequest Service { get; set; }
 | 
				
			||||||
 | 
					        public HttpStatusCode StatusCode { get; set; }
 | 
				
			||||||
 | 
					        public string Response { get; set; }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										9
									
								
								Birdmap.API/DTOs/ServiceRequest.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								Birdmap.API/DTOs/ServiceRequest.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,9 @@
 | 
				
			|||||||
 | 
					namespace Birdmap.API.DTOs
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    public class ServiceRequest
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        public int Id { get; set; }
 | 
				
			||||||
 | 
					        public string Name { get; set; }
 | 
				
			||||||
 | 
					        public string Uri { get; set; }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -1,5 +1,7 @@
 | 
				
			|||||||
using AutoMapper;
 | 
					using AutoMapper;
 | 
				
			||||||
 | 
					using Birdmap.API.DTOs;
 | 
				
			||||||
using Birdmap.DAL.Entities;
 | 
					using Birdmap.DAL.Entities;
 | 
				
			||||||
 | 
					using System;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace Birdmap.API.MapperProfiles
 | 
					namespace Birdmap.API.MapperProfiles
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
@@ -7,10 +9,31 @@ namespace Birdmap.API.MapperProfiles
 | 
				
			|||||||
    {
 | 
					    {
 | 
				
			||||||
        public BirdmapProfile()
 | 
					        public BirdmapProfile()
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            CreateMap<User, DTOs.AuthenticateResponse>()
 | 
					            CreateMap<Uri, string>().ConvertUsing<UriToStringConverter>();
 | 
				
			||||||
 | 
					            CreateMap<string, Uri>().ConvertUsing<UriToStringConverter>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            CreateMap<User, AuthenticateResponse>()
 | 
				
			||||||
                .ForMember(m => m.Username, opt => opt.MapFrom(m => m.Name))
 | 
					                .ForMember(m => m.Username, opt => opt.MapFrom(m => m.Name))
 | 
				
			||||||
                .ForMember(m => m.UserRole, opt => opt.MapFrom(m => m.Role))
 | 
					                .ForMember(m => m.UserRole, opt => opt.MapFrom(m => m.Role))
 | 
				
			||||||
                .ReverseMap();
 | 
					                .ReverseMap();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            CreateMap<Service, ServiceRequest>()
 | 
				
			||||||
 | 
					                .ReverseMap();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        private class UriToStringConverter : ITypeConverter<Uri, string>, ITypeConverter<string, Uri>
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            public string Convert(Uri source, string destination, ResolutionContext context)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                destination = source.ToString();
 | 
				
			||||||
 | 
					                return destination;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            public Uri Convert(string source, Uri destination, ResolutionContext context)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                Uri.TryCreate(source, UriKind.Absolute, out destination);
 | 
				
			||||||
 | 
					                return destination;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										15
									
								
								Birdmap.BLL/Interfaces/IServiceService.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								Birdmap.BLL/Interfaces/IServiceService.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,15 @@
 | 
				
			|||||||
 | 
					using Birdmap.DAL.Entities;
 | 
				
			||||||
 | 
					using System.Collections.Generic;
 | 
				
			||||||
 | 
					using System.Threading.Tasks;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace Birdmap.BLL.Interfaces
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    public interface IServiceService
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        Task<List<Service>> GetAllServicesAsync();
 | 
				
			||||||
 | 
					        Task<Service> GetServiceAsync(int id);
 | 
				
			||||||
 | 
					        Task<Service> CreateServiceAsync(Service service);
 | 
				
			||||||
 | 
					        Task DeleteServiceAsync(int id);
 | 
				
			||||||
 | 
					        Task UpdateServiceAsync(Service service);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										55
									
								
								Birdmap.BLL/Services/ServiceService.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										55
									
								
								Birdmap.BLL/Services/ServiceService.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,55 @@
 | 
				
			|||||||
 | 
					using Birdmap.BLL.Exceptions;
 | 
				
			||||||
 | 
					using Birdmap.BLL.Interfaces;
 | 
				
			||||||
 | 
					using Birdmap.DAL;
 | 
				
			||||||
 | 
					using Birdmap.DAL.Entities;
 | 
				
			||||||
 | 
					using Microsoft.EntityFrameworkCore;
 | 
				
			||||||
 | 
					using System.Collections.Generic;
 | 
				
			||||||
 | 
					using System.Threading.Tasks;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace Birdmap.BLL.Services
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    public class ServiceService : IServiceService
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        private readonly BirdmapContext _context;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public ServiceService(BirdmapContext context)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            _context = context;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public async Task<Service> CreateServiceAsync(Service service)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            _context.Services.Add(service);
 | 
				
			||||||
 | 
					            await _context.SaveChangesAsync();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            return service;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public Task DeleteServiceAsync(int id)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            var service = _context.Services.Find(id);
 | 
				
			||||||
 | 
					            if (service == null)
 | 
				
			||||||
 | 
					                return Task.CompletedTask;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            _context.Services.Remove(service);
 | 
				
			||||||
 | 
					            return _context.SaveChangesAsync();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public Task<List<Service>> GetAllServicesAsync()
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            return _context.Services.ToListAsync();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public async Task<Service> GetServiceAsync(int id)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            return await _context.Services.SingleOrDefaultAsync(s => s.Id == id)
 | 
				
			||||||
 | 
					                ?? throw new EntityNotFoundException($"Cannot find service with id: {id}.");
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public Task UpdateServiceAsync(Service service)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            _context.Services.Update(service);
 | 
				
			||||||
 | 
					            return _context.SaveChangesAsync();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -11,6 +11,7 @@ namespace Birdmap.BLL
 | 
				
			|||||||
        {
 | 
					        {
 | 
				
			||||||
            services.AddTransient<IAuthService, AuthService>();
 | 
					            services.AddTransient<IAuthService, AuthService>();
 | 
				
			||||||
            services.AddTransient<IUserService, UserService>();
 | 
					            services.AddTransient<IUserService, UserService>();
 | 
				
			||||||
 | 
					            services.AddTransient<IServiceService, ServiceService>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            return services;
 | 
					            return services;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,7 +2,6 @@
 | 
				
			|||||||
using Microsoft.Extensions.Configuration;
 | 
					using Microsoft.Extensions.Configuration;
 | 
				
			||||||
using Microsoft.Extensions.Logging;
 | 
					using Microsoft.Extensions.Logging;
 | 
				
			||||||
using System;
 | 
					using System;
 | 
				
			||||||
using System.Collections.Generic;
 | 
					 | 
				
			||||||
using System.Linq;
 | 
					using System.Linq;
 | 
				
			||||||
using static Birdmap.Common.PasswordHelper;
 | 
					using static Birdmap.Common.PasswordHelper;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user