Note Onion - 3 - Infrastructure - NoteTypeRepository

Here is the source code for NoteTypeRepository.cs file it is placed in the Repositories directory of the Note.Infrastructure project.

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Note.Domain.Entities;
using Note.Domain.Repositories;
using Note.Infrastructure.Data;
using Note.Shared;

namespace Note.Infrastructure.Repositories
{
public class NoteTypeRepository :
BaseRepository<NoteTypeRepository>,
INoteTypeRepository
{
private readonly NoteDataContext _dbContext;

public NoteTypeRepository(NoteDataContext dbContext,
ILogger<NoteTypeRepository> logger) : base(logger)
{
_dbContext = dbContext;
}

public async Task<IResult<List<NoteTypeEntity>>> GetNoteTypes(string companyKey, string applicationKey)
{
//return await _dbContext.NoteTypes
// .Where(nt => nt.CompanyKey == companyKey && nt.ApplicationKey == applicationKey)
// .ToListAsync();

try
{
var dbEntity = await _dbContext.NoteTypes
.Where(n => n.CompanyKey == companyKey &&
n.ApplicationKey == applicationKey )
.ToListAsync();

if (dbEntity == null)
return new Result<List<NoteTypeEntity>>().ReturnFail(LogWarning("NoteType Not Found"));

return new Result<List<NoteTypeEntity>>(dbEntity).ReturnSuccess(LogInformation("Obtained NoteType record"));
}
catch (Exception e)
{
return new Result<List<NoteTypeEntity>>().ReturnFail(e.Message, 0);
}
}

public async Task<IResult<NoteTypeEntity>> GetNoteTypeById(string companyKey, long id)
{
//return await _dbContext.NoteTypes.FindAsync(id);
if (id < 0)
return new Result<NoteTypeEntity>().ReturnFail(LogWarning("Invalid Id"), id);
try
{
var dbEntity = await _dbContext.NoteTypes
.FindAsync(id);

if (dbEntity == null)
return new Result<NoteTypeEntity>().ReturnFail(LogWarning("NoteType Not Found"));

return new Result<NoteTypeEntity>(dbEntity).ReturnSuccess(LogInformation("Obtained NoteType record"));
}
catch (Exception ex)
{
return new Result<NoteTypeEntity>().ReturnFail(LogError("Error trying to retrieve Note", ex), id);
}
}

public async Task<IBaseResult> CreateNoteType(NoteTypeEntity noteType)
{
//_dbContext.NoteTypes.Add(noteType);
//await _dbContext.SaveChangesAsync();
//return noteType.Id;

try
{
_dbContext.Add(noteType);
_dbContext.Entry(noteType).State = EntityState.Added;

var numUpdated = _dbContext.SaveChanges();
if (numUpdated > 0)
return new Result().ReturnSuccess(LogInformation("NoteType added Successfully"));
else
return new Result().ReturnFail(LogError("Error adding NoteType"));
}
catch (Exception ex)
{
return new Result().ReturnFail(LogError("Error adding NoteType!", ex), noteType.Id);
}

}

public async Task<IBaseResult> UpdateNoteType(NoteTypeEntity noteType)
{
//_dbContext.Entry(noteType).State = EntityState.Modified;
//await _dbContext.SaveChangesAsync();

try
{
_dbContext.NoteTypes.Update(noteType);
_dbContext.Entry(noteType).State = EntityState.Modified;
//_context.Entry(noteEntity.IdEntity).State = EntityState.Unchanged;

if (_dbContext.SaveChanges() > 0)
return new Result().ReturnSuccess(LogInformation("Updated NoteType record"));

return new Result().ReturnFail(LogError("Error updating NoteType record"), noteType.Id);
}
catch (Exception ex)
{
return new Result().ReturnFail(LogError("Error updating NoteType record!", ex), noteType.Id);
}

}

public async Task<IBaseResult> DeleteNoteType(string companyKey, long id)
{
//var noteType = await _dbContext.NoteTypes.FindAsync(id);
//if (noteType != null)
//{
// _dbContext.NoteTypes.Remove(noteType);
// await _dbContext.SaveChangesAsync();
//}
try
{
var noteTypeEntity = await GetNoteTypeById(companyKey, id);

_dbContext.NoteTypes.Remove(noteTypeEntity.Model);
if (_dbContext.SaveChanges() > 0)
return new Result().ReturnSuccess(LogInformation("Deleted NoteType record"));

return new Result().ReturnFail(LogError("Error deleting NoteType record"), id);
}
catch (Exception ex)
{
return new Result().ReturnFail(LogError("Error deleting NoteType record!", ex), id);
}

}
}
}