-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBaseRepository.cs
More file actions
57 lines (49 loc) · 1.54 KB
/
BaseRepository.cs
File metadata and controls
57 lines (49 loc) · 1.54 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
namespace TaskTracker.Api.Models
{
public interface IMicroserviceRepository
{
IEnumerable<Microservice> GetMicroservices();
object GetMicroserviceById(int id);
int CreateMicroservice(Microservice data);
string DeleteMicroService(int id);
}
public class MicroserviceRepository:IMicroserviceRepository
{
private readonly AppDBContext _context;
public MicroserviceRepository(AppDBContext context)
{
_context = context;
if(_context.Microservices.Count() == 0){
_context.Microservices.Add(new Microservice {Name = "Identity"});
_context.SaveChangesAsync();
}
}
public IEnumerable<Microservice> GetMicroservices()
{
var services = _context.Microservices.ToList();
return services;
}
public object GetMicroserviceById(int id)
{
var service = _context.Microservices.SingleOrDefault(x => x.Id == id);
return service;
}
public int CreateMicroservice(Microservice service)
{
_context.Microservices.Add(service);
_context.SaveChangesAsync();
return service.Id;
}
public string DeleteMicroService(int id)
{
var service = _context.Microservices.SingleOrDefault(x => x.Id == id);
if(service == null) return "Not Found";
_context.Microservices.Remove(service);
_context.SaveChangesAsync();
return "Deleted";
}
}
}