/// <summary> /// Updates the one song by identifier. /// </summary> /// <returns>Return the error code</returns> /// <param name="id">Identifier.</param> /// <param name="song">Song.</param> public async Task <int> UpdateOneSongById(int id, Song song) { int result = ErrorCodeModel.ErrorBecauseBugs; if (id == 0) { // its a bug, id of song cannot be 0 result = ErrorCodeModel.ErrorBecauseBugs; return(result); } if (song == null) { // the data for updating is empty result = ErrorCodeModel.SongIsNull; return(result); } if (string.IsNullOrEmpty(song.SongNo)) { // the song no that input by user is empty result = ErrorCodeModel.SongNoIsEmpty; return(result); } Song newSong = await FindOneSongBySongNo(song.SongNo); if (newSong != null) { if (newSong.Id != id) { // song no is duplicate result = ErrorCodeModel.SongNoDuplicate; return(result); } } Song orgSong = await FindOneSongById(id); if (orgSong == null) { // the original song does not exist any more result = ErrorCodeModel.OriginalSongNotExist; return(result); } else { orgSong.CopyColumnsFrom(song); // verifying the validation for Song data int validCode = await VerifySong(orgSong); if (validCode != ErrorCodeModel.Succeeded) { // data is invalid result = validCode; return(result); } // check if entry state changed if ((_context.Entry(orgSong).State) == EntityState.Modified) { using (var dbTransaction = _context.Database.BeginTransaction()) { try { await _context.SaveChangesAsync(); dbTransaction.Commit(); result = ErrorCodeModel.Succeeded; // succeeded to update } catch (DbUpdateException ex) { string msg = ex.ToString(); Console.WriteLine("Failed to update song table: \n" + msg); dbTransaction.Rollback(); result = ErrorCodeModel.DatabaseError; } } } else { result = ErrorCodeModel.SongNotChanged; // no changed } } return(result); }
/// <summary> /// Updates the one singarea by identifier. /// </summary> /// <returns>Return the error code</returns> /// <param name="id">Identifier.</param> /// <param name="singarea">Singarea.</param> public async Task <int> UpdateOneSingareaById(int id, Singarea singarea) { int result = ErrorCodeModel.ErrorBecauseBugs; if (id == 0) { // its a bug, id of singarea cannot be 0 result = ErrorCodeModel.ErrorBecauseBugs; return(result); } if (singarea == null) { // the data for updating is empty result = ErrorCodeModel.SingareaIsNull; return(result); } if (string.IsNullOrEmpty(singarea.AreaNo)) { // the singarea no that input by user is empty result = ErrorCodeModel.SingareaNoIsEmpty; return(result); } Singarea newSingarea = await FindOneSingareaByAreaNo(singarea.AreaNo); if (newSingarea != null) { if (newSingarea.Id != id) { // singarea no is duplicate result = ErrorCodeModel.SingareaNoDuplicate; return(result); } } Singarea orgSingarea = await FindOneSingareaById(id); if (orgSingarea == null) { // the original singarea does not exist any more result = ErrorCodeModel.OriginalSingareaNotExist; return(result); } else { orgSingarea.CopyColumnsFrom(singarea); // check if entry state changed if ((_context.Entry(orgSingarea).State) == EntityState.Modified) { using (var dbTransaction = _context.Database.BeginTransaction()) { try { await _context.SaveChangesAsync(); dbTransaction.Commit(); result = ErrorCodeModel.Succeeded; // succeeded to update } catch (DbUpdateException ex) { string msg = ex.ToString(); Console.WriteLine("Failed to update singarea table: \n" + msg); dbTransaction.Rollback(); result = ErrorCodeModel.DatabaseError; } } } else { result = ErrorCodeModel.SingareaNotChanged; // no changed } } return(result); }
/// <summary> /// Updates the one playerscore by identifier. /// </summary> /// <returns>Return the error code</returns> /// <param name="id">Identifier.</param> /// <param name="playerscore">Playerscore.</param> public async Task <int> UpdateOnePlayerscoreById(int id, Playerscore playerscore) { int result = ErrorCodeModel.ErrorBecauseBugs; if (id == 0) { // its a bug, id of playerscore cannot be 0 result = ErrorCodeModel.ErrorBecauseBugs; return(result); } if (playerscore == null) { // the data for updating is empty result = ErrorCodeModel.PlayerscoreIsNull; return(result); } if (string.IsNullOrEmpty(playerscore.PlayerName)) { // the playerscore name that input by user is empty result = ErrorCodeModel.PlayerNameIsEmpty; return(result); } Playerscore orgPlayerscore = await FindOnePlayerscoreById(id); if (orgPlayerscore == null) { // the original playerscore does not exist any more result = ErrorCodeModel.OriginalPlayerscoreNotExist; return(result); } else { orgPlayerscore.CopyFrom(playerscore); // check if entry state changed if ((_context.Entry(orgPlayerscore).State) == EntityState.Modified) { using (var dbTransaction = _context.Database.BeginTransaction()) { try { await _context.SaveChangesAsync(); dbTransaction.Commit(); result = ErrorCodeModel.Succeeded; // succeeded to update } catch (DbUpdateException ex) { string msg = ex.ToString(); Console.WriteLine("Failed to update Playerscore table: \n" + msg); dbTransaction.Rollback(); result = ErrorCodeModel.DatabaseError; } } } else { result = ErrorCodeModel.PlayerscoreNotChanged; // no changed } } return(result); }