Note Onion - 2 - Application - NoteService

Here is the source code for NoteService.cs file it is placed in the Services directory of the Note.Application 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
using System.Collections.Generic;
using System.Threading.Tasks;
using AutoMapper;
using Note.Domain.Repositories;
using Note.Application.DTOs;
using Note.Application.Services;
using Note.Domain.Entities;
using Note.Shared;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;

namespace Note.Application.Services
{
public class NoteService : BaseService<NoteService>, INoteService
{
private readonly INoteRepository _noteRepository;
private readonly IMapper _mapper;

public NoteService(INoteRepository noteRepository,
ILogger<NoteService> logger,
IMapper mapper) : base(logger)
{
_noteRepository = noteRepository;
_mapper = mapper;
}

/// <summary>
/// Returns a list of all notes
/// </summary>
/// <remarks>
/// A result(NoteVM) object containing a collection of
/// note type objects.
///
/// Get: api/v1/Notes
/// </remarks>
public async Task<IResult<List<NoteDto>>> GetAllNotesAsync(string companyKey, string applicationKey, string applicationSubKey)
{
try
{
var noteList = _noteRepository.GetAllNotes(companyKey, applicationKey, applicationSubKey);

if (noteList.Result.WasSuccessful)
return new Result<List<NoteDto>>(_mapper.Map<List<NoteDto>>(noteList.Result.Model)).ReturnSuccess(noteList.Result.Message.FriendlyMessage);
else
return new Result<List<NoteDto>>().ReturnFail(noteList.Result.Message.FriendlyMessage);
}
catch (Exception e)
{
return new Result<List<NoteDto>>().ReturnFail(
LogError("Internal error processing the request. Please contact admin.", e));
}
}

public async Task<IResult<NoteDto>> GetNoteById(string companyKey, long id)
{
//var note = await _noteRepository.GetNoteById(companyKey, id);
//var noteDto = _mapper.Map<NoteDto>(note);
//var x = BaseResult<NoteDto>.SuccessResponse(noteDto);
//return Task<x>;
try
{
var e = await _noteRepository.GetNoteById(companyKey, id);

if (!e.WasSuccessful)
return new Result<NoteDto>(_mapper.Map<NoteDto>(e.Model)).ReturnFail(e.Message.FriendlyMessage, id);
else
return new Result<NoteDto>(_mapper.Map<NoteDto>(e.Model)).ReturnSuccess(e.Message.FriendlyMessage);
}
catch (Exception e)
{
return new Result<NoteDto>().ReturnFail(LogError("There was a system error in getting Note", e), id);
}

}

public async Task<IBaseResult> CreateNote(NoteDto noteDto)
{
try
{ // todo: what do we do with ipAddress?

var note = _mapper.Map<NoteEntity>(noteDto);
var id = await _noteRepository.CreateNote(note);
//return new Result<IBaseResult>.ReturnSuccess(null, id);
return (IBaseResult)await Task.FromResult(id);

//var noteResult = _noteRepository.Add(_mapper.Map<NoteEntity>(noteViewModel));
//return noteResult;
}
catch (Exception ex)
{
return new Result().ReturnFail(LogError("Error adding Note to database!", ex),
noteDto.Id);
}

}

public async Task<IBaseResult> UpdateNote(NoteDto noteDto)
{
//var note = _mapper.Map<NoteEntity>(noteDto);
//await _noteRepository.UpdateNote(note);
//return Task<IBaseResult>.SuccessResponse(true);
try
{ // todo: what do we do with ipAddress?
var noteResult = await _noteRepository.UpdateNote(_mapper.Map<NoteEntity>(noteDto));

return (IBaseResult) noteResult;
}
catch (Exception ex)
{
return new Result().ReturnFail(LogError("Error Updating Note to database!", ex),
noteDto.Id);
}
}

public async Task<IBaseResult> DeleteNote(string companyKey, long id)
{
try
{
var noteResult = await _noteRepository.DeleteNote(companyKey, id);
return noteResult;
}
catch (Exception ex)
{
return new Result().ReturnFail(LogError("Error deleting Note from database!", ex), id);
}

}
}
}