/// <summary> /// Update a Copy or create a Copy if it doesn't exist. This means existing items will be overwritten. /// </summary> /// <param name="copy">The Copy to write to the database.</param> /// <returns>Returns true if one row was written and no error occured.</returns> static public bool Upsert(Copy copy) { try { using (SqlConnection connection = HelperFunctions.GetConnection()) { connection.Open(); using (SqlCommand command = new SqlCommand("EXEC UpsertCopy @Barcode, @Location, @StatusId, @ISBN, @Library")) { command.Connection = connection; command.Parameters.AddWithValue("@Barcode", copy.Barcode); command.Parameters.AddWithValue("@Location", HelperFunctions.ValueOrDBNull(copy.Location)); command.Parameters.AddWithValue("@StatusId", copy.StatusId); command.Parameters.AddWithValue("@ISBN", HelperFunctions.ValueOrDBNull(copy.ISBN)); command.Parameters.AddWithValue("@Library", HelperFunctions.ValueOrDBNull(copy.Library)); if (command.ExecuteNonQuery() != 1) { return false; } } } } catch (Exception) { return false; } return true; }
/// <summary> /// Fetch a Copy from the database. /// </summary> /// <param name="copy">A variable of type Copy. Will be set to a new instance if a record was found. /// Will be null after execution if no record was found.</param> /// <param name="barcode">The Barcode of the Copy to fetch.</param> /// <returns>Returns true if no error occured.</returns> public static bool GetCopy(out Copy copy, string barcode) { SqlCommand command = new SqlCommand("SELECT * from COPY WHERE Barcode = @Barcode"); command.Parameters.AddWithValue("@Barcode", barcode); var copies = new List<Copy>(); var result = getCopies(out copies, command); copy = result && copies.Count > 0 ? copies[0] : null; return result; }
/// <summary> /// Get the copy with given barcode. /// </summary> /// <param name="barcode">The barcode of the copy.</param> /// <returns>Returns a CopyViewModel.</returns> /// <exception cref="Services.Exceptions.DoesNotExistException"> /// Thrown when no copy with given barcode could be found.</exception> /// <exception cref="Services.Exceptions.DataAccessException"> /// Thrown when an error occurs in the data access layer.</exception> public static CopyViewModel GetCopyViewModel(string barcode) { var cvm = new CopyViewModel(); var copy = new Copy(); if (Copy.GetCopy(out copy, barcode)) { if (copy == null) throw new DoesNotExistException("Ett exemplar med angiven streckkod finns inte i databasen."); cvm = Mapper.Map<CopyViewModel>(copy); } else throw new DataAccessException("Oväntat fel när ett exemplar skulle hämtas."); return cvm; }