Note Onion - 3 - Infrastructure - NoteRepository

Here is the source code for NoteRepository.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
143
144
145
146
147
148
149
150
151
152
153
154
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 NoteRepository : BaseRepository<NoteRepository>, INoteRepository
{
private readonly NoteDataContext _dbContext;

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

public async Task<IResult<List<NoteEntity>>> GetAllNotes(
string companyKey,
string applicationKey,
string applicationSubKey)
{
try
{
//return await Task<IResult<List<NoteEntity>>>(
// _dbContext.Notes
// .Where(n => n.CompanyKey == companyKey &&
// n.ApplicationKey == applicationKey &&
// n.ApplicationSubKey == applicationSubKey)
// .Include(n => n.NoteTypeEntity)
// .ToListAsync()
// ).ReturnSuccess(LogInformation("Retrieved all Notes records successfully"));

var dbEntity = await _dbContext.Notes
.Where(n => n.CompanyKey == companyKey &&
n.ApplicationKey == applicationKey &&
n.ApplicationSubKey == applicationSubKey)
.Include(n => n.NoteTypeEntity)
.ToListAsync();

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

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

public async Task<IResult<NoteEntity>> GetNoteById(string companyKey, long id)
{
if (id < 0)
return new Result<NoteEntity>().ReturnFail(LogWarning("Invalid Id"), id);
try
{
var dbEntity = _dbContext.Notes
.Include(r => r.NoteTypeEntity)
.FirstOrDefault(e => e.Id == id);

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

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

public async Task<IBaseResult> CreateNote(NoteEntity noteEntity)
{
try
{
_dbContext.Add(noteEntity);
_dbContext.Entry(noteEntity).State = EntityState.Added;
_dbContext.Entry(noteEntity.NoteTypeEntity).State = EntityState.Unchanged;

var numUpdated = _dbContext.SaveChanges();
if (numUpdated > 0)
return new Result().ReturnSuccess(LogInformation("Note added Successfully"));

return new Result().ReturnFail(LogError("Error adding Note"));
}
catch (Exception ex)
{
return new Result().ReturnFail(LogError("Error adding Note!", ex), noteEntity.Id);
}
}

public async Task<IBaseResult> UpdateNote(NoteEntity noteEntity)
{
//_dbContext.Entry(note).State = EntityState.Modified;
//await _dbContext.SaveChangesAsync();
try
{
noteEntity.NoteTypeId = noteEntity.NoteTypeEntity.Id;
_dbContext.Notes.Update(noteEntity);
//_dbContext.Entry(noteEntity).State = EntityState.Modified;
_dbContext.Entry(noteEntity.NoteTypeEntity).State = EntityState.Unchanged;
//_context.Entry(noteEntity.IdEntity).State = EntityState.Unchanged;

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

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

public async Task<IBaseResult> DeleteNote(string companyKey, long id)
{
//var note = await _dbContext.Notes.FindAsync(id);
//if (note != null)
//{
// _dbContext.Notes.Remove(note);
// await _dbContext.SaveChangesAsync();
//}

try
{
var noteEntity = await GetNoteById(companyKey, id);

_dbContext.Notes.Remove(noteEntity.Model);
var saveResults = _dbContext.SaveChanges();
if (saveResults > 0)
{
return new Result().ReturnSuccess(LogInformation("Deleted Note record"));

//var logResult = await LogInformation("Deleted Note record");
//return new Result().ReturnSuccess(logResult);
}


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

}
}
}