Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
ASPNETCoreDockerMicroservices/Services/Applicants.Api/Services/ApplicantRepository.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
54 lines (47 sloc)
1.69 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Data; | |
using System.Data.SqlClient; | |
using System.Threading.Tasks; | |
using Applicants.Api.Models; | |
using Dapper; | |
namespace Applicants.Api.Services | |
{ | |
public interface IApplicantRepository | |
{ | |
Task<IEnumerable<Applicant>> GetAll(); | |
Task<int> AddApplicantSubmission(ApplicantSubmission applicantSubmission); | |
} | |
public class ApplicantRepository : IApplicantRepository | |
{ | |
private readonly string _connectionString; | |
public ApplicantRepository(string connectionString) | |
{ | |
_connectionString = connectionString; | |
} | |
public IDbConnection Connection => new SqlConnection(_connectionString); | |
public async Task<IEnumerable<Applicant>> GetAll() | |
{ | |
using (var dbConnection = Connection) | |
{ | |
dbConnection.Open(); | |
return await dbConnection.QueryAsync<Applicant>("SELECT * FROM Applicants"); | |
} | |
} | |
public async Task<int> AddApplicantSubmission(ApplicantSubmission applicantSubmission) | |
{ | |
using (var dbConnection = Connection) | |
{ | |
dbConnection.Open(); | |
return await dbConnection.ExecuteAsync( | |
"insert ApplicantSubmissions values(@jobId,@applicantId,@title,@submissionDate)", | |
new | |
{ | |
jobId = applicantSubmission.JobId, | |
applicantId = applicantSubmission.ApplicantId, | |
title = applicantSubmission.Title, | |
submissionDate = applicantSubmission.SubmissionDate | |
}); | |
} | |
} | |
} | |
} |