public async Task <IActionResult> AddWorkspaceCourseAsync(WorkspaceCourseEditModel model) { try { var userId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value; if (userId == null) { throw new AppException("UC1001"); } var workspaceId = User.Claims.FirstOrDefault(c => c.Type == "WorkspaceId")?.Value; if (string.IsNullOrWhiteSpace(workspaceId)) { throw new AppException("UC1001"); } var isManager = User.Claims.Any(c => c.Type == ClaimTypes.Role && c.Value == "Manager"); // TODO: Upload image and return path var imageUrl = "http://placehold.it/300"; var course = new Course { Name = model.Name, Code = model.Code, Fee = model.Fee, ImageUrl = imageUrl, Description = model.Description, CategoryId = model.CategoryId, WorkspaceId = int.Parse(workspaceId) }; if (!isManager) { course.CourseUsers = new List <CourseUser> { new CourseUser { UserId = int.Parse(userId), RoleCode = "Instructor" } }; } var createdCourse = await _courseService.CreateWorkspaceCourseAsync(course, int.Parse(workspaceId)); return(Ok(new { id = createdCourse.Id, name = createdCourse.Name, code = createdCourse.Code, fee = createdCourse.Fee, imageurl = createdCourse.ImageUrl, description = createdCourse.Description })); } catch (Exception ex) { return(BadRequest(new { ex.Message })); } }
public async Task <IActionResult> UpdateWorkspaceCourseAsync(int courseId, WorkspaceCourseEditModel model) { try { var userId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value; if (userId == null) { throw new AppException("UC1001"); } var workspaceId = User.Claims.FirstOrDefault(c => c.Type == "WorkspaceId")?.Value; if (string.IsNullOrWhiteSpace(workspaceId)) { throw new AppException("UC1001"); } var isManager = User.Claims.Any(c => c.Type == ClaimTypes.Role && c.Value == "Manager"); var course = await _courseService.GetCourseByWorkspaceIdAsync(courseId, int.Parse(workspaceId), isManager?(int?)null : int.Parse(userId)); if (course == null) { throw new AppException("UC1001"); } course.Name = model.Name; course.Code = model.Code; course.Fee = model.Fee; course.Description = model.Description; course.CategoryId = model.CategoryId; var updatedCourse = await _courseService.UpdateWorkspaceCourseAsync(course, int.Parse(workspaceId)); return(Ok(new { id = updatedCourse.Id, name = updatedCourse.Name, code = updatedCourse.Code, fee = updatedCourse.Fee, imageurl = updatedCourse.ImageUrl, description = updatedCourse.Description })); } catch (Exception ex) { return(BadRequest(new { ex.Message })); } }