Note Onion - 2 - Application - NoteTypeService

Here is the source code for NoteTypeService.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
using System.Collections.Generic;
using System.Threading.Tasks;
using AutoMapper;
using Note.Application.DTOs;
using Note.Domain.Repositories;
using Note.Application.Services;
using Note.Domain.Entities;
using Note.Shared;
using Microsoft.Extensions.Logging;

namespace Note.Application.Services
{
public class NoteTypeService : BaseService<NoteTypeService>, INoteTypeService
{
private readonly INoteTypeRepository _noteTypeRepository;
private readonly IMapper _mapper;

public NoteTypeService(INoteTypeRepository noteTypeRepository,
ILogger<NoteTypeService> logger,
IMapper mapper) : base(logger)
{
_noteTypeRepository = noteTypeRepository;
_mapper = mapper;
}

public async Task<IResult<List<NoteTypeDto>>> GetNoteTypes(string companyKey, string applicationKey)
{
try
{
var noteList = _noteTypeRepository.GetNoteTypes(companyKey, applicationKey);

if (noteList.Result.WasSuccessful)
return new Result<List<NoteTypeDto>>(_mapper.Map<List<NoteTypeDto>>(noteList.Result.Model)).ReturnSuccess(noteList.Result.Message.FriendlyMessage);

return new Result<List<NoteTypeDto>>().ReturnFail(noteList.Result.Message.FriendlyMessage);
}
catch (Exception e)
{
return new Result<List<NoteTypeDto>>().ReturnFail(
LogError("Internal error processing the request. Please contact admin.", e));
}
}

public async Task<IResult<NoteTypeDto>> GetNoteTypeById(string companyKey, long id)
{
try
{
var e = await _noteTypeRepository.GetNoteTypeById(companyKey, id);

if (!e.WasSuccessful)
return new Result<NoteTypeDto>(_mapper.Map<NoteTypeDto>(e.Model)).ReturnFail(e.Message.FriendlyMessage, id);

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

}

public async Task<IBaseResult> CreateNoteType(NoteTypeDto noteTypeDto)
{
try
{ // todo: what do we do with ipAddress?

var noteType = _mapper.Map<NoteTypeEntity>(noteTypeDto);
var id = await _noteTypeRepository.CreateNoteType(noteType);
return id;
}
catch (Exception ex)
{
return new Result().ReturnFail(LogError("Error adding NoteType to database!", ex),
noteTypeDto.Id);
}
}

public async Task<IBaseResult> UpdateNoteType(NoteTypeDto noteTypeDto)
{
try
{ // todo: what do we do with ipAddress?
var noteResult = await _noteTypeRepository.UpdateNoteType(_mapper.Map<NoteTypeEntity>(noteTypeDto));

return (IBaseResult) noteResult;
}
catch (Exception ex)
{
return new Result().ReturnFail(LogError("Error adding NoteType to database!", ex),
noteTypeDto.Id);
}
}

public async Task<IBaseResult> DeleteNoteType(string companyKey, long id)
{
try
{
var noteTypeResult = await _noteTypeRepository.DeleteNoteType(companyKey, id);

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

}