public IActionResult CreateBorrowRecord(int userId, int tapeId) { // Check if input model is valid, output all errors if not if (!ModelState.IsValid) { IEnumerable <string> errorList = ModelState.Values.SelectMany(v => v.Errors).Select(x => x.ErrorMessage); throw new InputFormatException("User input model improperly formatted.", errorList); } _tapeService.CreateBorrowRecord(tapeId, userId); return(Created($"{userId}/tapes/{tapeId}", null)); }
/// <summary> /// Creates borrow record from tapes for user /// </summary> /// <param name="userJSON">json object for user</param> /// <param name="tapeService">tape service with tape functionalities</param> public static void CreateTapesForUser(dynamic userJSON, ITapeService tapeService) { // Create all borrows associated with user after user was added if (userJSON.tapes != null) { foreach (var borrowRecord in userJSON.tapes) { // Generate input model from json for borrow record BorrowRecordInputModel record = ConvertJSONToBorrowRecordInputModel(borrowRecord); // Check if borrow record input model is valid var results = new List <ValidationResult>(); var context = new ValidationContext(record, null, null); if (!Validator.TryValidateObject(record, context, results)) { IEnumerable <string> errorList = results.Select(x => x.ErrorMessage); throw new InputFormatException("Tapes borrow for user in initialization file improperly formatted.", errorList); } // Otherwise add to database tapeService.CreateBorrowRecord((int)borrowRecord.id, (int)userJSON.id, record); } } }
public void CreateBorrowRecord_ShouldThrowNotFoundForUserId() => _tapeService.CreateBorrowRecord(_tapeMockListSize, _userMockListSize + 1, It.IsAny <BorrowRecordInputModel>());