public async Task <ResponseModel> UpdatePublishBookAsync(Guid id, PublishBookManageModel publishBookManageModel) { var publishBook = await GetAll().FirstOrDefaultAsync(x => x.Id == id); if (publishBook == null) { return(new ResponseModel() { StatusCode = System.Net.HttpStatusCode.NotFound, Message = "This PublishBook is not exist. Please try again!" }); } else { await _lecturerInPublishBookRepository.DeleteAsync(publishBook.LecturerInPublishBooks); var lecturerInPublishBooks = new List <LecturerInPublishBook>(); foreach (var lecturerId in publishBookManageModel.LecturerIds) { lecturerInPublishBooks.Add(new LecturerInPublishBook() { PublishBookId = publishBook.Id, LecturerId = lecturerId }); } _lecturerInPublishBookRepository.GetDbContext().LecturerInPublishBooks.AddRange(lecturerInPublishBooks); await _lecturerInPublishBookRepository.GetDbContext().SaveChangesAsync(); publishBookManageModel.GetPublishBookFromModel(publishBook); await _publishBookResponstory.UpdateAsync(publishBook); publishBook = await GetAll().FirstOrDefaultAsync(x => x.Id == id); return(new ResponseModel { StatusCode = System.Net.HttpStatusCode.OK, Data = new PublishBookViewModel(publishBook) }); } }
public async Task <ResponseModel> CreatePublishBookAsync(PublishBookManageModel publishBookManageModel) { var publishBook = await _publishBookResponstory.FetchFirstAsync(x => x.Name == publishBookManageModel.Name && x.BookCategoryId == publishBookManageModel.BookCategoryId); if (publishBook != null) { return(new ResponseModel() { StatusCode = System.Net.HttpStatusCode.BadRequest, Message = "This PublishBook is exist. Can you try again with the update!" }); } else { publishBook = _mapper.Map <PublishBook>(publishBookManageModel); var bookCategory = await _bookCategoryRepository.GetByIdAsync(publishBookManageModel.BookCategoryId); publishBook.BookCategory = bookCategory; await _publishBookResponstory.InsertAsync(publishBook); var lecturerInPublishBooks = new List <LecturerInPublishBook>(); foreach (var lecturerId in publishBookManageModel.LecturerIds) { lecturerInPublishBooks.Add(new LecturerInPublishBook() { PublishBookId = publishBook.Id, LecturerId = lecturerId }); } _lecturerInPublishBookRepository.GetDbContext().LecturerInPublishBooks.AddRange(lecturerInPublishBooks); await _lecturerInPublishBookRepository.GetDbContext().SaveChangesAsync(); return(new ResponseModel() { StatusCode = System.Net.HttpStatusCode.OK, Data = new PublishBookViewModel(publishBook) }); } }
public async Task <IActionResult> Update(Guid id, [FromBody] PublishBookManageModel publishBookManageModel) { var response = await _publishBookService.UpdatePublishBookAsync(id, publishBookManageModel); return(new CustomActionResult(response)); }
public async Task <IActionResult> Create([FromBody] PublishBookManageModel publishBookManagerModel) { var response = await _publishBookService.CreatePublishBookAsync(publishBookManagerModel); return(new CustomActionResult(response)); }