Skip to content

Commit dcf974c

Browse files
committed
Produce API using ASP.Net
Implement Entity Framework, produce API that have several features such as register, login, change password, reset password, etc, with authentication, authorization, and CORS
1 parent 448a150 commit dcf974c

File tree

62 files changed

+4490
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+4490
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using ImplementCors.Models;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.Extensions.Logging;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Diagnostics;
7+
using System.Linq;
8+
using System.Threading.Tasks;
9+
10+
namespace ImplementCors.Controllers
11+
{
12+
public class HomeController : Controller
13+
{
14+
private readonly ILogger<HomeController> _logger;
15+
16+
public HomeController(ILogger<HomeController> logger)
17+
{
18+
_logger = logger;
19+
}
20+
21+
public IActionResult Index()
22+
{
23+
return View();
24+
}
25+
26+
public IActionResult Privacy()
27+
{
28+
return View();
29+
}
30+
31+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
32+
public IActionResult Error()
33+
{
34+
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
35+
}
36+
}
37+
}

ImplementCors/Controllers/TestCORS.cs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
7+
namespace ImplementCors.Controllers
8+
{
9+
public class TestCORS : Controller
10+
{
11+
public IActionResult Index()
12+
{
13+
return View();
14+
}
15+
}
16+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace ImplementCors.Models
4+
{
5+
public class ErrorViewModel
6+
{
7+
public string RequestId { get; set; }
8+
9+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
10+
}
11+
}

ImplementCors/Program.cs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Microsoft.AspNetCore.Hosting;
2+
using Microsoft.Extensions.Configuration;
3+
using Microsoft.Extensions.Hosting;
4+
using Microsoft.Extensions.Logging;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Threading.Tasks;
9+
10+
namespace ImplementCors
11+
{
12+
public class Program
13+
{
14+
public static void Main(string[] args)
15+
{
16+
CreateHostBuilder(args).Build().Run();
17+
}
18+
19+
public static IHostBuilder CreateHostBuilder(string[] args) =>
20+
Host.CreateDefaultBuilder(args)
21+
.ConfigureWebHostDefaults(webBuilder =>
22+
{
23+
webBuilder.UseStartup<Startup>();
24+
});
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:2086",
7+
"sslPort": 44301
8+
}
9+
},
10+
"profiles": {
11+
"IIS Express": {
12+
"commandName": "IISExpress",
13+
"launchBrowser": true,
14+
"environmentVariables": {
15+
"ASPNETCORE_ENVIRONMENT": "Development"
16+
}
17+
},
18+
"ImplementCors": {
19+
"commandName": "Project",
20+
"launchBrowser": true,
21+
"applicationUrl": "https://localhost:5001;http://localhost:5000",
22+
"environmentVariables": {
23+
"ASPNETCORE_ENVIRONMENT": "Development"
24+
}
25+
}
26+
}
27+
}

ImplementCors/Startup.cs

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.AspNetCore.Hosting;
3+
using Microsoft.AspNetCore.HttpsPolicy;
4+
using Microsoft.Extensions.Configuration;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using Microsoft.Extensions.Hosting;
7+
using System;
8+
using System.Collections.Generic;
9+
using System.Linq;
10+
using System.Threading.Tasks;
11+
12+
namespace ImplementCors
13+
{
14+
public class Startup
15+
{
16+
public Startup(IConfiguration configuration)
17+
{
18+
Configuration = configuration;
19+
}
20+
21+
public IConfiguration Configuration { get; }
22+
23+
// This method gets called by the runtime. Use this method to add services to the container.
24+
public void ConfigureServices(IServiceCollection services)
25+
{
26+
services.AddControllersWithViews();
27+
}
28+
29+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
30+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
31+
{
32+
if (env.IsDevelopment())
33+
{
34+
app.UseDeveloperExceptionPage();
35+
}
36+
else
37+
{
38+
app.UseExceptionHandler("/Home/Error");
39+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
40+
app.UseHsts();
41+
}
42+
app.UseHttpsRedirection();
43+
app.UseStaticFiles();
44+
45+
app.UseRouting();
46+
47+
app.UseAuthorization();
48+
49+
app.UseEndpoints(endpoints =>
50+
{
51+
endpoints.MapControllerRoute(
52+
name: "default",
53+
pattern: "{controller=Home}/{action=Index}/{id?}");
54+
});
55+
}
56+
}
57+
}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
@{
3+
Layout = null;
4+
}
5+
6+
<!DOCTYPE html>
7+
8+
<html>
9+
<head>
10+
<meta name="viewport" content="width=device-width" />
11+
<title>Index</title>
12+
</head>
13+
<body>
14+
</body>
15+
</html>
16+
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
17+
<script>
18+
19+
$.ajax({
20+
url: "https://localhost:44300/api/persons/GetPerson/",
21+
success: function (result) {
22+
console.log(result);
23+
}
24+
})
25+
</script>

ImplementCors/appsettings.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft": "Warning",
6+
"Microsoft.Hosting.Lifetime": "Information"
7+
}
8+
},
9+
"AllowedHosts": "*"
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft": "Warning",
6+
"Microsoft.Hosting.Lifetime": "Information"
7+
}
8+
},
9+
"AllowedHosts": "*"
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// <autogenerated />
2+
using System;
3+
using System.Reflection;
4+
[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETCoreApp,Version=v3.1", FrameworkDisplayName = "")]

NETCore/Base/BaseController.cs

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
using Microsoft.AspNetCore.Http;
2+
using Microsoft.AspNetCore.Mvc;
3+
using NETCore.Repository;
4+
using NETCore.Repository.Data;
5+
using NETCore.Repository.Interface;
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using System.Net;
10+
using System.Threading.Tasks;
11+
12+
namespace NETCore.Base
13+
{
14+
[Route("api/[controller]")]
15+
[ApiController]
16+
public class BaseController<Entity, Repository, Key> : ControllerBase
17+
where Entity : class
18+
where Repository : IRepository<Entity, Key>
19+
20+
{
21+
private readonly Repository repository;
22+
23+
public BaseController(Repository repository)
24+
{
25+
this.repository = repository;
26+
}
27+
28+
[HttpGet]
29+
public ActionResult Get()
30+
{
31+
32+
return Ok(new
33+
{
34+
status = 200,
35+
data = repository.Get(),
36+
message = "Data berhasil Di tampilkan"
37+
});
38+
}
39+
40+
[HttpGet("{key}")]
41+
public ActionResult Get(Key key)
42+
{
43+
if (repository.Get(key) != null)
44+
{
45+
46+
//return Ok(personRepository.Get(NIK));
47+
return Ok(new
48+
{
49+
status = HttpStatusCode.OK,
50+
data = repository.Get(key),
51+
message = "Data berhasil Di tampilkan"
52+
});
53+
}
54+
return NotFound(new
55+
{
56+
status = HttpStatusCode.NotFound,
57+
message = "Data dengan ID/NIK Tersebut Tidak Ditemukan"
58+
//return Ok(personRepository.Get(NIK));
59+
});
60+
61+
}
62+
63+
[HttpPost]
64+
public ActionResult Insert(Entity entity)
65+
{
66+
if (repository.Insert(entity) != 0)
67+
{
68+
return Ok(new
69+
{
70+
status = HttpStatusCode.OK,
71+
message = "Data berhasil Di Tambahkan"
72+
});
73+
}
74+
else
75+
{
76+
return BadRequest(new
77+
{
78+
status = HttpStatusCode.BadRequest,
79+
message = "Gagal menambahkan data, periksa kembali"
80+
});
81+
}
82+
}
83+
84+
[HttpPut]
85+
public ActionResult Update(Entity entity)
86+
{
87+
try
88+
{
89+
if (repository.Update(entity) != 0)
90+
{
91+
//return Ok("Data Berhasil di Update");
92+
return Ok(new
93+
{
94+
status = HttpStatusCode.OK,
95+
message = "Data berhasil Di Update"
96+
});
97+
}
98+
}
99+
catch (Exception e)
100+
{
101+
Console.WriteLine(e.Message);
102+
}
103+
return NotFound(new
104+
{
105+
status = HttpStatusCode.NotFound,
106+
message = "Data dengan ID/NIK tersebut Tidak Ditemukan"
107+
108+
});
109+
}
110+
111+
[HttpDelete("{key}")]
112+
public ActionResult Delete(Key key)
113+
{
114+
if (repository.Get(key) != null)
115+
{
116+
return Ok(new
117+
{
118+
status = HttpStatusCode.OK,
119+
data = repository.Get(key),
120+
deletedata = repository.Delete(key),
121+
message = "Data berhasil Di Hapus"
122+
});
123+
}
124+
else
125+
{
126+
return NotFound(new
127+
{
128+
status = HttpStatusCode.NotFound,
129+
message = "Data dengan ID/NIK tersebut Tidak Ditemukan"
130+
});
131+
}
132+
133+
}
134+
135+
}
136+
}

0 commit comments

Comments
 (0)