public async Task <ApiResult> GetByIdAsync(int id) { var result = default(ApiResult); var course = await courses.GetByIdAsync(id); if (course == null) { var message = "Course not found"; var errors = new string[] { $"Course with id = {id} was not found" }; result = ApiResult.GetErrorResult(ApiResultStatus.NotFound, message, message, errors); } else { var data = mapperWrapper.MapModel(course); data.Cover = imageWorker.GetImageURL("courses", data.Cover); result = ApiResult.GetOkResult(ApiResultStatus.Ok, data: data); } return(result); }
private async Task <AccountData> GetAccountData(SystemUser user) { var code = Encoding.UTF8.GetBytes(configuration["JwtKey"]); var roles = await userManager.GetRolesAsync(user); var token = JWTHelper.GenerateJwtToken(user, configuration, tokenHandler, code, roles); var expire = Convert.ToDouble(configuration["JwtExpireDays"]); var userDTO = mapperWrapper.MapModel(user); var avatarURL = imageWorker.GetImageURL("users", user.AvatarPath); userDTO.AvatarPath = avatarURL; var data = new AccountData(userDTO, new TokenData(token, expire)); return(data); }
public async Task <ApiResult> UploadImageAsync(IFormFile image, int id) { var result = default(ApiResult); var fileName = imageWorker.ImageUploader.UploadImage(image, "users"); if (string.IsNullOrWhiteSpace(fileName)) { var message = "An error occured during file upload"; result = ApiResult.GetErrorResult(ApiResultStatus.BadRequest, "Error in upload file for USER", message, new string[] { message }); } else { var user = await users.GetByIdAsync(id); if (user == null) { var message = "User was not found"; result = ApiResult.GetErrorResult(ApiResultStatus.NotFound, $"Error: user id={id} was not found in UPLOAD FILE", message, new string[] { message }); } else { if (!string.IsNullOrWhiteSpace(user.AvatarPath)) { var imagePath = imageWorker.GetImageRootPath("users", user.AvatarPath); imageWorker.DeleteImage(imagePath); } user.AvatarPath = fileName; await context.SaveChangesAsync(); var model = mapperWrapper.MapModel(user); var avatarPath = imageWorker.GetImageURL("users", fileName); model.AvatarPath = avatarPath; result = ApiResult.GetOkResult(ApiResultStatus.Ok, "Image was uploaded", model); } } return(result); }