Skip to content

Commit

Permalink
API added
Browse files Browse the repository at this point in the history
  • Loading branch information
Ezgi Yaman committed Jan 5, 2022
1 parent 0a5b104 commit 492c9cd
Show file tree
Hide file tree
Showing 9 changed files with 373 additions and 1 deletion.
12 changes: 11 additions & 1 deletion Content-Management-System.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CMS.Infrastructure", "src\W
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CMS.Application", "src\Web\CMS.Application\CMS.Application.csproj", "{0E0882F9-6464-4696-A1DE-14F2931FA26B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CMS.Prensentation", "src\Web\CMS.Prensentation\CMS.Prensentation.csproj", "{097F7634-B354-4909-98D0-A5C8B47328C5}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CMS.Prensentation", "src\Web\CMS.Prensentation\CMS.Prensentation.csproj", "{097F7634-B354-4909-98D0-A5C8B47328C5}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "API", "API", "{E1EE2A71-49B3-4413-AA90-7174B5E16504}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CMS.API", "src\API\CMS.API\CMS.API.csproj", "{EFE6A09B-15D7-4319-A584-AF6AA7873E07}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -37,6 +41,10 @@ Global
{097F7634-B354-4909-98D0-A5C8B47328C5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{097F7634-B354-4909-98D0-A5C8B47328C5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{097F7634-B354-4909-98D0-A5C8B47328C5}.Release|Any CPU.Build.0 = Release|Any CPU
{EFE6A09B-15D7-4319-A584-AF6AA7873E07}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EFE6A09B-15D7-4319-A584-AF6AA7873E07}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EFE6A09B-15D7-4319-A584-AF6AA7873E07}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EFE6A09B-15D7-4319-A584-AF6AA7873E07}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -47,6 +55,8 @@ Global
{17D002E3-EA0C-4995-994C-F92708072C82} = {AC4923FC-0BF0-4BCD-86BE-01A65562C662}
{0E0882F9-6464-4696-A1DE-14F2931FA26B} = {AC4923FC-0BF0-4BCD-86BE-01A65562C662}
{097F7634-B354-4909-98D0-A5C8B47328C5} = {AC4923FC-0BF0-4BCD-86BE-01A65562C662}
{E1EE2A71-49B3-4413-AA90-7174B5E16504} = {DD3732DD-2D6F-4C0D-A137-9DB3DEDA9BC9}
{EFE6A09B-15D7-4319-A584-AF6AA7873E07} = {E1EE2A71-49B3-4413-AA90-7174B5E16504}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {CA195274-EDD7-40CF-AC8C-EE1EB368EDEE}
Expand Down
24 changes: 24 additions & 0 deletions src/API/CMS.API/CMS.API.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
<DocumentationFile>C:\Users\YAMAN\source\repos\Content-Management-System\src\API\CMS.API\CMS.API.xml</DocumentationFile>
<NoWarn>1701;1702;1591</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.2.3" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.2.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Web\CMS.Application\CMS.Application.csproj" />
<ProjectReference Include="..\..\Web\CMS.Infrastructure\CMS.Infrastructure.csproj" />
</ItemGroup>


</Project>
46 changes: 46 additions & 0 deletions src/API/CMS.API/CMS.API.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

111 changes: 111 additions & 0 deletions src/API/CMS.API/Controllers/CategoryController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using CMS.Application.Models.DTOs;
using CMS.Application.Models.VMs;
using CMS.Application.Services.Interface;
using CMS.Domain.Entities.Concrete;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CMS.API.Controllers
{
[Produces("application/json")] //Sadece tanımlanan türlerle sınırlanır.XML ile çalışmasını isteseydim ; "application/xml")] yazılırdı.
[Route("api/[controller]")]
[ApiController]
public class CategoryController : ControllerBase
{
private readonly ICategoryService _categoryService;

public CategoryController(ICategoryService categoryService)
{
_categoryService = categoryService;
}

/// <summary>
/// Get All Categories
/// </summary>
/// <returns></returns>
[HttpGet]
public async Task<List<GetCategoryVM>> GetCategories()
{
//Getcategories methoddu zaten bana list döndürüyor o yüzden direk list döndük getcategories git bak
var categoryList = await _categoryService.GetCategories();

return categoryList;
}

/// <summary>
/// Get Category By Id
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("{id:int}", Name = "GetCategoryById")]
[ProducesResponseType(200)] //status code ok başarılı dönmesi için ekledik.

//category ıd'sinden yakalayı getirme işlemi
public async Task<UpdateCategoryDTO> GetCategoryById(int id)
{
var category = await _categoryService.GetById(id);
return category;
}


/// <summary>
/// Get one category by slug
/// </summary>
/// <param name="slug"></param>
/// <returns></returns>
[HttpGet("{slug}", Name = "GetCategoryBySlug")]
public async Task<Category> GetCategoryBySlug(string slug)
{
var category = await _categoryService.GetBySlug(slug);

return category;
}
/// <summary>
/// Create a category
/// </summary>
/// <param name="model">
/// Please check category model in the schemas table
/// </param>
/// <returns></returns>
[HttpPost]
public async Task<ActionResult<Category>> CreateCategory([FromBody] CreateCategoryDTO model)
{ //action result olarak işaretleyince bad request ve ok kabul görüyor yazmsak bize kızıyor."[FromBody]" atacağımız request'in gövdesinde veriyi göndereceğimiz zaman tercih ediyoruz.
if (ModelState.IsValid)
{
await _categoryService.Create(model);
return Ok();
}
return BadRequest();
}

/// <summary>
/// Update one category.Category slug must be uniqe.
/// </summary>
/// <param name="model">
/// Please check category model in the shemas table
/// </param>
/// <returns></returns>
[HttpPut]
public async Task<ActionResult<Category>> UpdateCategory([FromBody] UpdateCategoryDTO model)
{
if (ModelState.IsValid)
{
await _categoryService.Update(model);
return Ok(model);
}
return BadRequest(model);

}

[HttpDelete("{id}")]
public async Task<IActionResult> DeleteCategory(int id)
{
await _categoryService.Delete(id);
return Ok();
}
}
}
34 changes: 34 additions & 0 deletions src/API/CMS.API/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Autofac;
using Autofac.Extensions.DependencyInjection;
using CMS.Application.IoC;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CMS.API
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureContainer<ContainerBuilder>(builder =>
{
builder.RegisterModule(new DependencyResolver());
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
29 changes: 29 additions & 0 deletions src/API/CMS.API/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:24461",
"sslPort": 0
}
},
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"CMS.API": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "weatherforecast",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:5000"
}
}
}
96 changes: 96 additions & 0 deletions src/API/CMS.API/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using CMS.Infrastructure.Context;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace CMS.API
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();

services.AddDbContext<AppDbContext>(options =>
{

options.UseSqlServer(Configuration.GetConnectionString("ProjectContext"));
});

//Swagger Resolve
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("CMSAPI", new OpenApiInfo()
{
Title = "CMS API",
Version = "v1.1",
Description = "Restful API E",
Contact = new OpenApiContact()
{
Email = "[email protected]",
Name = "Ezgi Yaman",
Url = new Uri("https://github.com/ezgiyaman")
},
License = new OpenApiLicense()
{
Name = "MIT Licanse",
Url = new Uri("https://opensource.org/licenses/MIT")
}
});


//summary için ekledim.
var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
});

}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

//swagger kullandýðým için endpoint'ini ekledim.

app.UseSwagger();
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/swagger/CMSAPI/swagger.json", "Restful API");
options.RoutePrefix = string.Empty;
});

app.UseRouting();

app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
9 changes: 9 additions & 0 deletions src/API/CMS.API/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
Loading

0 comments on commit 492c9cd

Please sign in to comment.