public async Task <IActionResult> Edit([FromBody] BookInstResource bookInstRes) { var valResult = await resourceClientValidator.IsValidAsync(bookInstRes); if (!valResult.Success) { return(BadRequest(valResult.ErrorMessage)); } var toUpdate = await getBookInst(bookInstRes.Id); if (toUpdate == null) { return(BadRequest("No book instance found with the id : " + bookInstRes.Id)); } var bookInst = mapper.Map <BookInstResource, BookInst>(bookInstRes); updateFields(toUpdate, bookInst); await uow.CompleteAsync(); var justUpdated = await this.repo.Get(bookInstRes.Id); return(Ok(justUpdated)); }
public async Task <Result> IsValidAsync(BookInstResource res) { var writers = await this.bookDefRepo.GetAll(); if (!writers.Any(w => w.Id == res.DefinitionId)) { return(Result.Fail("The instance of a book must have valid definition id")); } if (res.LanguageId.HasValue && res.LanguageId > 0) { var langs = await miscRepo.GetLanguages(); if (!langs.Any(l => l.Id == res.LanguageId)) { return(Result.Fail("Invalid LanguageId. You must provide a valid LanguageId or no LanguageId (zero)")); } } if (res.PublisherId.HasValue && res.PublisherId > 0) { var pubs = await miscRepo.GetPublishers(); if (!pubs.Any(l => l.Id == res.PublisherId)) { return(Result.Fail("Invalid PublisherId. You must provide a valid PublisherId or no PublisherId (zero)")); } } if (res.TranslatorId.HasValue && res.TranslatorId > 0) { var trns = await miscRepo.GetTranslators(); if (!trns.Any(l => l.Id == res.TranslatorId)) { return(Result.Fail("Invalid TranslatorId. You must provide a valid TranslatorId or no TranslatorId (zero)")); } } return(Result.Succeed()); }
public async Task <IActionResult> Create([FromBody] BookInstResource bookInstRes) { var valResult = await resourceClientValidator.IsValidAsync(bookInstRes); if (!valResult.Success) { return(BadRequest(valResult.ErrorMessage)); } // Redundant alanları set edelim tanımlardan var bookInst = mapper.Map <BookInstResource, BookInst>(bookInstRes); var defs = await definitionRepo.GetAll(); bookInst.WriterName = defs.FirstOrDefault(a => a.Id == bookInst.DefinitionId).Writer.Name; bookInst.BookName = defs.FirstOrDefault(a => a.Id == bookInst.DefinitionId).Name; repo.Create(bookInst); var bookInsts = await this.repo.GetAll(); var justCreated = bookInsts.Where(a => a.Definition.Name == bookInst.Definition.Name); return(Ok(justCreated)); }