public async Task <ResponseModel> UpdateScientificReportAsync(Guid id, ScientificReportManageModel scientificReportManageModel) { var scientificReport = await GetAll().FirstOrDefaultAsync(x => x.Id == id); if (scientificReport == null) { return(new ResponseModel() { StatusCode = System.Net.HttpStatusCode.NotFound, Message = "This ScientificReport is not exist. Please try again!" }); } else { await _lecturerInScientificReportRepository.DeleteAsync(scientificReport.LecturerInScientificReports); var lecturerInScientificReports = new List <LecturerInScientificReport>(); foreach (var lecturerId in scientificReportManageModel.LecturerIds) { lecturerInScientificReports.Add(new LecturerInScientificReport() { ScientificReportId = scientificReport.Id, LecturerId = lecturerId }); } _lecturerInScientificReportRepository.GetDbContext().LecturerInScientificReports.AddRange(lecturerInScientificReports); await _lecturerInScientificReportRepository.GetDbContext().SaveChangesAsync(); scientificReportManageModel.GetScientificReportFromModel(scientificReport); await _scientificReportResponstory.UpdateAsync(scientificReport); scientificReport = await GetAll().FirstOrDefaultAsync(x => x.Id == id); return(new ResponseModel { StatusCode = System.Net.HttpStatusCode.OK, Data = new ScientificReportViewModel(scientificReport) }); } }
//public async Task<ResponseModel> CreateScientificReportAsync(ScientificReportManageModel scientificReportManageModel) //{ // var scientificReport = await _scientificReportResponstory.FetchFirstAsync(x => x.Name == scientificReportManageModel.Name && x.ScientificReportTypeId == scientificReportManageModel.ScientificReportTypeId); // if (scientificReport != null) // { // return new ResponseModel() // { // StatusCode = System.Net.HttpStatusCode.BadRequest, // Message = "This ScientificReport is exist. Can you try again with the update!" // }; // } // else // { // var scientificReportType = await _scientificReportTypeRepository.GetByIdAsync(scientificReportManageModel.ScientificReportTypeId); // var lecturer = await _lecturerRepository.GetByIdAsync(scientificReportManageModel.LecturerId); // scientificReport = _mapper.Map<ScientificReport>(scientificReportManageModel); // scientificReport.ScientificReportType = scientificReportType; // scientificReport.Lecturer = lecturer; // await _scientificReportResponstory.InsertAsync(scientificReport); // return new ResponseModel() // { // StatusCode = System.Net.HttpStatusCode.OK, // Data = new ScientificReportViewModel(scientificReport) // }; // } //} public async Task <ResponseModel> CreateScientificReportAsync(ScientificReportManageModel scientificReportManageModel) { var scientificReport = await _scientificReportResponstory.FetchFirstAsync(x => x.Name == scientificReportManageModel.Name && x.ScientificReportTypeId == scientificReportManageModel.ScientificReportTypeId); if (scientificReport != null) { return(new ResponseModel() { StatusCode = System.Net.HttpStatusCode.BadRequest, Message = "This ScientificReport is exist. Can you try again with the update!" }); } else { scientificReport = _mapper.Map <ScientificReport>(scientificReportManageModel); var scientificReportType = await _scientificReportTypeRepository.GetByIdAsync(scientificReportManageModel.ScientificReportTypeId); scientificReport.ScientificReportType = scientificReportType; await _scientificReportResponstory.InsertAsync(scientificReport); var lecturerInScientificReports = new List <LecturerInScientificReport>(); foreach (var lecturerId in scientificReportManageModel.LecturerIds) { lecturerInScientificReports.Add(new LecturerInScientificReport() { ScientificReportId = scientificReport.Id, LecturerId = lecturerId }); } _lecturerInScientificReportRepository.GetDbContext().LecturerInScientificReports.AddRange(lecturerInScientificReports); await _lecturerInScientificReportRepository.GetDbContext().SaveChangesAsync(); return(new ResponseModel() { StatusCode = System.Net.HttpStatusCode.OK, Data = new ScientificReportViewModel(scientificReport) }); } }
public async Task <IActionResult> Update(Guid id, [FromBody] ScientificReportManageModel scientificReportManageModel) { var response = await _scientificReportService.UpdateScientificReportAsync(id, scientificReportManageModel); return(new CustomActionResult(response)); }
public async Task <IActionResult> Create([FromBody] ScientificReportManageModel scientificReportManagerModel) { var response = await _scientificReportService.CreateScientificReportAsync(scientificReportManagerModel); return(new CustomActionResult(response)); }