-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e843a69
commit 42acc62
Showing
10 changed files
with
211 additions
and
5 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using Business.Communication; | ||
using Business.Domain.Services; | ||
using Business.Resources; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace API.Controllers | ||
{ | ||
[ApiController] | ||
[Route("api/v1/timesheet")] | ||
public class TimesheetController : ControllerBase | ||
{ | ||
#region Property | ||
private readonly ITimesheetService _timesheetService; | ||
protected readonly ResponseMessage ResponseMessage; | ||
#endregion | ||
|
||
#region Constructor | ||
public TimesheetController(ITimesheetService timesheetService, | ||
IOptionsMonitor<ResponseMessage> responseMessage) | ||
{ | ||
this._timesheetService = timesheetService; | ||
this.ResponseMessage = responseMessage.CurrentValue; | ||
} | ||
#endregion | ||
|
||
#region Action | ||
[HttpPost("import")] | ||
[AllowAnonymous] | ||
[ProducesResponseType(typeof(BaseResponse<>), StatusCodes.Status200OK)] | ||
[ProducesResponseType(typeof(BaseResponse<>), StatusCodes.Status400BadRequest)] | ||
public async Task<IActionResult> ImportAsync([FromForm] IFormFile file) | ||
{ | ||
// Validate file import | ||
var validateResult = ValidateTimesheet(file); | ||
if (!validateResult.isSuccess) return BadRequest(validateResult.result); | ||
|
||
var filePath = Path.GetTempFileName(); | ||
|
||
var stream = new FileStream(filePath, FileMode.Create); | ||
await file.CopyToAsync(stream); | ||
stream.Position = 0; | ||
|
||
var result = await _timesheetService.ImportAsync(stream); | ||
stream.Dispose(); | ||
|
||
// Clean temp-file | ||
if (System.IO.File.Exists(filePath)) | ||
System.IO.File.Delete(filePath); | ||
|
||
if (result.Success) | ||
return Ok(result); | ||
|
||
return BadRequest(result); | ||
} | ||
#endregion | ||
|
||
#region Private work | ||
private (bool isSuccess, BaseResponse<object> result) ValidateTimesheet(IFormFile file) | ||
{ | ||
if (file == null || file.Length <= 0) | ||
return (false, new BaseResponse<object>(ResponseMessage.Values["File_Empty"])); | ||
|
||
if (!Path.GetExtension(file.FileName).Equals(".xlsx", StringComparison.OrdinalIgnoreCase)) | ||
return (false, new BaseResponse<object>(ResponseMessage.Values["Not_Support_File"])); | ||
|
||
return (true, new BaseResponse<object>(true)); | ||
} | ||
#endregion | ||
} | ||
} |
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using Business.Communication; | ||
using Business.Resources.Timesheet; | ||
|
||
namespace Business.Domain.Services | ||
{ | ||
public interface ITimesheetService | ||
{ | ||
Task<BaseResponse<TimesheetResource>> ImportAsync(Stream stream); | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace Business.Resources.Timesheet | ||
{ | ||
public class TimesheetResource | ||
{ | ||
public int Id { get; set; } | ||
|
||
[Display(Name = "Total Work Day")] | ||
public float TotalWorkDay { get; set; } | ||
|
||
[Display(Name = "Work Day")] | ||
public float WorkDay { get; set; } | ||
public float? Absent { get; set; } | ||
public float? Holiday { get; set; } | ||
|
||
[Display(Name = "Unpaid Leave")] | ||
public float? UnpaidLeave { get; set; } | ||
|
||
[Display(Name = "Paid Leave")] | ||
public float? PaidLeave { get; set; } | ||
public string TimesheetJSON { get; set; } | ||
public DateTime Date { get; set; } | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using AutoMapper; | ||
using Business.Communication; | ||
using Business.Domain.Repositories; | ||
using Business.Domain.Services; | ||
using Business.Resources; | ||
using Business.Resources.Information; | ||
using Business.Resources.Timesheet; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Options; | ||
using OfficeOpenXml; | ||
|
||
namespace Business.Services | ||
{ | ||
public class TimesheetService : ResponseMessageService, ITimesheetService | ||
{ | ||
#region Property | ||
protected readonly IMapper Mapper; | ||
protected readonly IUnitOfWork UnitOfWork; | ||
private readonly HostResource _hostResource; | ||
private readonly IWebHostEnvironment _env; | ||
#endregion | ||
|
||
#region Constructor | ||
public TimesheetService( | ||
IMapper mapper, | ||
IUnitOfWork unitOfWork, | ||
IWebHostEnvironment env, | ||
IOptionsMonitor<HostResource> hostResource, | ||
IOptionsMonitor<ResponseMessage> responseMessage) : base(responseMessage) | ||
{ | ||
this.Mapper = mapper; | ||
this.UnitOfWork = unitOfWork; | ||
this._hostResource = hostResource.CurrentValue; | ||
this._env = env; | ||
} | ||
#endregion | ||
|
||
#region Method | ||
public async Task<BaseResponse<TimesheetResource>> ImportAsync(Stream stream) | ||
{ | ||
using (ExcelPackage package = new ExcelPackage()) | ||
{ | ||
await package.LoadAsync(stream); | ||
|
||
// Get the first worksheet in the workbook | ||
ExcelWorksheet worksheet = package.Workbook.Worksheets[0]; | ||
|
||
var year = (double?)worksheet.Cells[3, 2].Value; | ||
var month = (double?)worksheet.Cells[3, 4].Value; | ||
|
||
if (year == null || month == null) | ||
return new BaseResponse<TimesheetResource>(ResponseMessage.Values["Timesheet_Invalid"]); | ||
|
||
Console.WriteLine($"Timesheet - Year: {year}"); | ||
Console.WriteLine($"Timesheet - Month: {month}"); | ||
|
||
//int col = 2; // Column 2 is the item description | ||
//for (int row = 2; row < 5; row++) | ||
// Console.WriteLine("\tCell({0},{1}).Value={2}", row, col, worksheet.Cells[row, col].Value); | ||
|
||
string newPath = GetRootPath($"{year}-{month}.xlsx"); | ||
if (File.Exists(newPath)) | ||
File.Delete(newPath); | ||
|
||
await package.SaveAsAsync(newPath); | ||
} | ||
|
||
return new BaseResponse<TimesheetResource>(true); | ||
} | ||
|
||
#region Private work | ||
private string GetRootPath(string timesheetFileName) | ||
{ | ||
string timesheetPath = string.Format($"{_hostResource.TimesheetPath}{timesheetFileName}"); | ||
string rootPath = string.Concat(_env.WebRootPath, timesheetPath); | ||
|
||
return rootPath; | ||
} | ||
#endregion | ||
|
||
#endregion | ||
} | ||
|
||
} |