Skip to content

Commit

Permalink
update something
Browse files Browse the repository at this point in the history
  • Loading branch information
PhucNghi176 committed Jul 8, 2024
1 parent 5757515 commit f6e9a13
Show file tree
Hide file tree
Showing 20 changed files with 430 additions and 58 deletions.
11 changes: 11 additions & 0 deletions DeerCoffeeShop.API/Controllers/EmployeeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using DeerCoffeeShop.API.Services;
using DeerCoffeeShop.Application.Authentication.Login;
using DeerCoffeeShop.Application.Employees;
using DeerCoffeeShop.Application.Employees.AddDeviceToken;
using DeerCoffeeShop.Application.Employees.CreateEmployee;
using DeerCoffeeShop.Application.Employees.DeleteEmployee;
using DeerCoffeeShop.Application.Employees.GetAllEmployee;
Expand Down Expand Up @@ -83,5 +84,15 @@ public async Task<ActionResult<EmployeeDto>> GetAll([FromQuery] GetAllEmployeeQu
};
return Ok(response);
}
[HttpPost("add-token")]
public async Task<ActionResult<string>> AddDeviceToken(AddDeviceTokenCommand command, CancellationToken cancellationToken)
{
string result = await _sender.Send(command, cancellationToken);
var response = new
{
Message = result,
};
return Ok(response);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using DeerCoffeeShop.Application.EmployeeShift.GetByEmployeeId;
using DeerCoffeeShop.Application.EmployeeShift.GetEmployeeShiftInAWeek;
using DeerCoffeeShop.Application.EmployeeShift.GetNeededReviewShift;
using DeerCoffeeShop.Application.EmployeeShift.GetShiftByID;
using DeerCoffeeShop.Application.EmployeeShift.LockDay;
using MediatR;
using Microsoft.AspNetCore.Mvc;
Expand Down Expand Up @@ -214,5 +215,16 @@ public async Task<ActionResult<JsonResponse<string>>> LockDay([FromBody] LockDay
};
return Ok(respond);
}
[HttpGet("getbyid")]
public async Task<ActionResult<JsonResponse<EmployeeShiftDtoV2>>> GetShiftByID([FromQuery] GetShiftByIDQuery query, CancellationToken cancellationToken)
{
var result = await _mediator.Send(query, cancellationToken);
var respond = new
{
Message = "Get successfully",
Data = result
};
return Ok(respond);
}

}
35 changes: 35 additions & 0 deletions DeerCoffeeShop.API/Controllers/Form/FormController.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using DeerCoffeeShop.API.Controllers.ResponseTypes;
using DeerCoffeeShop.Application.Common.Pagination;
using DeerCoffeeShop.Application.Forms;
using DeerCoffeeShop.Application.Forms.Commands.AbsentForm;
using DeerCoffeeShop.Application.Forms.Commands.AcceptEmployeeAndGeneratePassword;
using DeerCoffeeShop.Application.Forms.Commands.AcceptFormAndSendMail;
using DeerCoffeeShop.Application.Forms.Commands.ChangeFormStatus;
using DeerCoffeeShop.Application.Forms.Queries.GetAbsentFormEmployee;
using DeerCoffeeShop.Application.Forms.Queries.GetAllPagination;
using MediatR;
using Microsoft.AspNetCore.Mvc;
Expand Down Expand Up @@ -45,4 +48,36 @@ public async Task<IActionResult> SendPassword([FromRoute] string id)
};
return Ok(response);
}
[HttpPost("day-off")]
public async Task<IActionResult> DayOffForm(AbsentFormCommand command)
{
string resutl = await _sender.Send(new AbsentFormCommand(command.ShiftID, command.Reason, command.FormType));
var response = new
{
Message = resutl
};
return Ok(response);
}
[HttpGet("absent-forms")]
public async Task<ActionResult<PagedResult<FormDto>>> GetAbsentForm([FromQuery] int pageNumber, int pageSize)
{
var result = await _sender.Send(new GetAbsentFormQuery(pageNumber, pageSize));
var response = new
{
Message = "Get Absent Form Successfully",
Data = result
};
return Ok(response);
}
[HttpPost("approve")]
public async Task<ActionResult<FormDto>> GetChangeShiftForm(ChangeFormStatusCommand command)
{
var result = await _sender.Send(new ChangeFormStatusCommand(command.FormID, command.IsApprove,command.Response));
var response = new
{
Message = "Get Change Shift Form Successfully",
Data = result
};
return Ok(response);
}
}
2 changes: 0 additions & 2 deletions DeerCoffeeShop.Application/Authentication/Login/LoginQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@ public record LoginQuery(string EmployeeID, string Password) : IRequest<LoginDTO
{
public string EmployeeID { get; } = EmployeeID;
public string Password { get; } = Password;


}
Original file line number Diff line number Diff line change
Expand Up @@ -75,26 +75,24 @@ public async Task<string> Handle(CheckOutCommand request, CancellationToken canc
}
private async Task<CloudinaryDotNet.Actions.ImageUploadResult> UploadEmployeeImage(IFormFile imageFile)
{
using (Stream stream = imageFile.OpenReadStream())
using Stream stream = imageFile.OpenReadStream();
ImageUploadParams uploadParams = new()
{
ImageUploadParams uploadParams = new()
{
File = new FileDescription(imageFile.FileName, stream),
UseFilename = true,
UniqueFilename = false,
Folder = "EmployeeCheckIn",
Overwrite = true
};
File = new FileDescription(imageFile.FileName, stream),
UseFilename = true,
UniqueFilename = false,
Folder = "EmployeeCheckIn",
Overwrite = true
};

try
{
return await cloudinary.UploadAsync(uploadParams);
}
catch (Exception ex)
{
// Log the exception or handle it as necessary
return null;
}
try
{
return await cloudinary.UploadAsync(uploadParams);
}
catch (Exception ex)
{
// Log the exception or handle it as necessary
return null;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ namespace DeerCoffeeShop.Application.EmployeeShift.Delete
[Authorize]
public class DeleteEmployeeShiftCommand() : IRequest<string>, ICommand
{
public string EmployeeID { get; set; }
public string RestaurantID { get; set; }
public int ShiftID { get; set; }
public string ShiftID { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,10 @@ public class DeleteEmployeeShiftCommandHandler(IEmployeeShiftRepository employee

public async Task<string> Handle(DeleteEmployeeShiftCommand request, CancellationToken cancellationToken)
{
Domain.Entities.EmployeeShift foundObject = await _employeeShiftRepository.FindAsync(x => x.EmployeeID.Equals(request.EmployeeID)
&& x.RestaurantID.Equals(request.RestaurantID)
var shift = await _employeeShiftRepository.FindAsync(x => x.ID == request.ShiftID, cancellationToken)?? throw new NotFoundException("Shift not found");
_employeeShiftRepository.Remove(shift);

&& x.NguoiXoaID == null) ?? throw new NotFoundException("None employee shift of restaurant was found!");

foundObject.EmployeeID = null;
foundObject.NguoiXoaID = _currentUserService.UserId;
foundObject.NgayXoa = DateTime.Now;

return await _employeeShiftRepository.UnitOfWork.SaveChangesAsync(cancellationToken) > 0 ? "Xóa thành công" : "Xóa thất bại";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,7 @@ public DeleteEmployeeShiftCommandValidator()

public void Configure()
{
_ = RuleFor(x => x.EmployeeID)
.NotEmpty()
.NotNull()
.WithMessage("Employee must not be empty!");

_ = RuleFor(x => x.RestaurantID)
.NotEmpty()
.NotNull()
.WithMessage("Restaurant must not be empty!");

_ = RuleFor(x => x.ShiftID)
.NotEmpty()
.NotNull()
.WithMessage("Shift must not be empty!");

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ public class EmployeeShiftDtoV2 : IMapFrom<Domain.Entities.EmployeeShift>
public int EmployeeNote { get; set; }
public string? Note { get; set; }
public EmployeeDto Employee { get; set; }
public string? CheckInUrl { get; set; }
public string? CheckOutUrl { get; set; }

public static EmployeeShiftDtoV2 Create(string ID, string RestaurantID, EmployeeDto employee,
DateOnly dateOfWork, int month, int year, DateTime checkIn, DateTime checkOut, DateTime? actual_CheckIn
, DateTime? actual_CheckOut, DateTime? totalHours, bool isOnTime, EmployeeShiftStatus status
, int employeeNote, string? note)
{
, int employeeNote, string? note, string? CheckInUrl, string? CheckOutUrl)
{
return new EmployeeShiftDtoV2()
{
ID = ID,
Expand All @@ -47,6 +49,8 @@ public static EmployeeShiftDtoV2 Create(string ID, string RestaurantID, Employee
Status = status,
EmployeeNote = employeeNote,
Note = note,
CheckInUrl = CheckInUrl,
CheckOutUrl = CheckOutUrl
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ public record GetEmployeeShiftInAWeekQuery : IRequest<List<EmployeeShiftDtoV2>>,
public DateOnly Date { get; set; }
public bool IsMonth { get; set; }
}
internal class GetEmployeeShiftInAWeekQueryHandler(IEmployeeShiftRepository employeeShiftRepository, ICurrentUserService currentUserService, IMapper mapper, IRestaurantRepository restaurantRepository, IEmployeeRepository employeeRepository) : IRequestHandler<GetEmployeeShiftInAWeekQuery, List<EmployeeShiftDtoV2>>
internal class GetEmployeeShiftInAWeekQueryHandler(IEmployeeShiftRepository employeeShiftRepository, ICurrentUserService currentUserService, IMapper mapper, IRestaurantRepository restaurantRepository, IEmployeeRepository employeeRepository, IAttdenceRepository attdenceRepository) : IRequestHandler<GetEmployeeShiftInAWeekQuery, List<EmployeeShiftDtoV2>>
{
private readonly IEmployeeShiftRepository _employeeShiftRepository = employeeShiftRepository;
private readonly ICurrentUserService _currentUserService = currentUserService;
private readonly IRestaurantRepository _restaurantRepository = restaurantRepository;
private readonly IEmployeeRepository _employeeRepository = employeeRepository;
private readonly IAttdenceRepository _attdenceRepository = attdenceRepository;
private readonly IMapper _mapper = mapper;

public async Task<List<EmployeeShiftDtoV2>> Handle(GetEmployeeShiftInAWeekQuery request, CancellationToken cancellationToken)
Expand Down Expand Up @@ -47,21 +48,23 @@ public async Task<List<EmployeeShiftDtoV2>> Handle(GetEmployeeShiftInAWeekQuery
}
else
{
employeeShifts = await _employeeShiftRepository.FindAllAsync(x => x.Month == request.Date.Month && x.RestaurantID == ManagerIDOfRestaurant.ID, cancellationToken);
Employee? User = await _employeeRepository.FindAsync(x => x.ID == UserID, cancellationToken);
employeeShifts = await _employeeShiftRepository.FindAllAsync(x => x.Month == request.Date.Month && x.RestaurantID == User.ManagerID, cancellationToken);
}

// Retrieve employee details
foreach (Domain.Entities.EmployeeShift item in employeeShifts)
{
item.Employee = await _employeeRepository.FindAsync(x => x.ID == item.EmployeeID, cancellationToken);
}
//var task = employeeShifts.Select(async item =>
//{
// item.Employee = await _employeeRepository.FindAsync(x => x.ID == item.EmployeeID, cancellationToken);
//}
// );
//await Task.WhenAll(task);
return _mapper.Map<List<EmployeeShiftDtoV2>>(employeeShifts);

var map = _mapper.Map<List<EmployeeShiftDtoV2>>(employeeShifts);

foreach (var item in map)
{
var attdence = await _attdenceRepository.FindAsync(x => x.EmployeeShiftID == item.ID, cancellationToken);
}
return map;

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using AutoMapper;
using DeerCoffeeShop.Domain.Common.Exceptions;
using DeerCoffeeShop.Domain.Repositories;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DeerCoffeeShop.Application.EmployeeShift.GetShiftByID;

public record GetShiftByIDQuery : IRequest<EmployeeShiftDtoV2>
{
public string ShiftID { get; set; }

}
internal sealed class GetShiftByIDQueryHandler : IRequestHandler<GetShiftByIDQuery, EmployeeShiftDtoV2>
{
private readonly IEmployeeShiftRepository _employeeShiftRepository;
private readonly IMapper _mapper;

public GetShiftByIDQueryHandler(IEmployeeShiftRepository employeeShiftRepository, IMapper mapper)
{
_employeeShiftRepository = employeeShiftRepository;
_mapper = mapper;
}

public async Task<EmployeeShiftDtoV2> Handle(GetShiftByIDQuery request, CancellationToken cancellationToken)
{
var shift = await _employeeShiftRepository.FindAsync(x => x.ID == request.ShiftID, cancellationToken) ?? throw new NotFoundException("Shift not found");
return _mapper.Map<EmployeeShiftDtoV2>(shift);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using DeerCoffeeShop.Domain.Repositories;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DeerCoffeeShop.Application.Employees.AddDeviceToken;

public record AddDeviceTokenCommand : IRequest<string>
{
public string EmployeeID { get; set; }
public string DeviceToken { get; set; }
}
internal sealed class AddDeviceTokenCommandHandler(IEmployeeRepository employeeRepository) : IRequestHandler<AddDeviceTokenCommand, string>
{
private readonly IEmployeeRepository _employeeRepository = employeeRepository;
public async Task<string> Handle(AddDeviceTokenCommand request, CancellationToken cancellationToken)
{
var employee = await _employeeRepository.FindAsync(x => x.ID == request.EmployeeID, cancellationToken);
employee.DeviceToken = request.DeviceToken;
_employeeRepository.Update(employee);
_ = await _employeeRepository.UnitOfWork.SaveChangesAsync(cancellationToken);
return "Add device token successfully!";

}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using DeerCoffeeShop.Application.Common.Interfaces;
using DeerCoffeeShop.Application.Common.Security;
using DeerCoffeeShop.Domain.Repositories;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DeerCoffeeShop.Application.Forms.Commands.AbsentForm
{
[Authorize]
public record AbsentFormCommand : IRequest<string>
{
public string ShiftID { get; set; }
public string Reason { get; set; }
public int FormType { get; set; }
public AbsentFormCommand(string shiftID, string reason, int formType)
{
ShiftID = shiftID;
Reason = reason;
FormType = formType;
}
}
internal sealed class AbsentFormCommandHandlder : IRequestHandler<AbsentFormCommand, string>
{
private readonly ICurrentUserService _currentUserService;
private readonly IEmployeeRepository _employeeRepository;
private readonly IFormRepository _formRepository;

public AbsentFormCommandHandlder(ICurrentUserService currentUserService, IEmployeeRepository employeeRepository, IFormRepository formRepository)
{
_currentUserService = currentUserService;
_employeeRepository = employeeRepository;
_formRepository = formRepository;
}

public async Task<string> Handle(AbsentFormCommand request, CancellationToken cancellationToken)
{
string? UserID = _currentUserService.UserId;

var form = new Domain.Entities.Form
{
EmployeeID = UserID,
ShiftID = request.ShiftID,
FormType = (Domain.Enums.FormTypeEnum)request.FormType,
Content = request.Reason,
Date = DateTimeOffset.UtcNow.ToOffset(TimeSpan.FromHours(7)).DateTime,
};
_formRepository.Add(form);
return await _formRepository.UnitOfWork.SaveChangesAsync(cancellationToken) > 0 ? "Sucess" : "Failed";



}
}
}
Loading

0 comments on commit f6e9a13

Please sign in to comment.