public async Task AddBookLibrary(BookLibraryDto input) { List <string> errorList = new List <string>(); var value = new BookLibrary { BookId = input.BookId, LibraryId = input.LibraryId, Stock = input.Stock }; BookLibraryValidator validator = new BookLibraryValidator(); ValidationResult validationResult = validator.Validate(value); var datas = _bookLibraryRepository.GetAll(); if (!validationResult.IsValid) { foreach (var failure in validationResult.Errors) { errorList.Add(string.Format("{0}", failure.ErrorMessage)); } string errorString = string.Join(" ", errorList.ToArray()); throw new UserFriendlyException(errorString); } var data = datas.Where(d => d.BookId == input.BookId && d.LibraryId == input.LibraryId).ToList(); if (data.Count != 0) { foreach (var ed in data) { ed.Stock = ed.Stock + input.Stock; await _bookLibraryRepository.UpdateAsync(ed); } } else { await _bookLibraryRepository.InsertAsync(value); } }
public async Task UpdateBookLibrary(BookLibraryDto input) { List <string> errorList = new List <string>(); var data = await GetBookLibraryById(input.Id); data.Stock = input.Stock; BookLibraryValidator validator = new BookLibraryValidator(); ValidationResult validationResult = validator.Validate(data); if (!validationResult.IsValid) { foreach (var failure in validationResult.Errors) { errorList.Add(string.Format("{0}", failure.ErrorMessage)); } string errorString = string.Join(" ", errorList.ToArray()); throw new UserFriendlyException(errorString); } await _bookLibraryRepository.UpdateAsync(data); }