/// <summary> /// Updates the Project. /// </summary> /// <param name="updatedProject">The updated Project.</param> /// <returns>An <see cref="IOpResult" /> object containing data including whether or /// not the operation was successful and any error messages.</returns> public IOpResult UpdateProject(Project updatedProject) { var result = new OpResult(); try { using (var ctx = new TimeTrackerContext()) { // Update the object var project = ctx.Projects.First(p => p.ProjectId == updatedProject.ProjectId); ctx.Entry(project).CurrentValues.SetValues(updatedProject); ctx.SaveChanges(); // Return success result.IsSuccessful = true; return result; } } catch (Exception ex) { // Return failure result.IsSuccessful = false; result.ErrorMessage = ex.Message; return result; } }
public IHttpActionResult PutToDo(int id, ToDo toDo) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != toDo.Id) { return(BadRequest()); } db.Entry(toDo).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!ToDoExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
/// <summary> /// Updates the client. /// </summary> /// <param name="updatedClient">The updated client.</param> /// <returns>An <see cref="IOpResult" /> object containing data including whether or /// not the operation was successful and any error messages.</returns> public IOpResult UpdateClient(Client updatedClient) { var result = new OpResult(); try { using (var ctx = new TimeTrackerContext()) { // Update the object var client = ctx.Clients.First(c => c.ClientId == updatedClient.ClientId); updatedClient.UserId = client.UserId; ctx.Entry(client).CurrentValues.SetValues(updatedClient); ctx.SaveChanges(); // Return success result.IsSuccessful = true; return result; } } catch (Exception ex) { // Return failure result.IsSuccessful = false; result.ErrorMessage = ex.Message; return result; } }
/// <summary> /// Updates the entry. /// </summary> /// <param name="updatedEntry">The updated entry.</param> /// <returns>An <see cref="IOpResult" /> object containing data including whether or /// not the operation was successful and any error messages.</returns> public IOpResult UpdateTimeEntry(TimeEntry updatedEntry) { var result = new OpResult(); try { using (var ctx = new TimeTrackerContext()) { // Update the object var entry = ctx.TimeEntries.Single(t => t.TimeEntryId == updatedEntry.TimeEntryId); updatedEntry.CreatedAt = entry.CreatedAt; updatedEntry.CreatedBy = entry.CreatedBy; updatedEntry.UserId = entry.UserId; ctx.Entry(entry).CurrentValues.SetValues(updatedEntry); ctx.SaveChanges(); // Return success result.IsSuccessful = true; return result; } } catch (Exception ex) { // Return failure result.IsSuccessful = false; result.ErrorMessage = ex.Message; return result; } }
/// <summary> /// Updates the billing term. /// </summary> /// <param name="updatedTerm">The updated term.</param> /// <returns> /// An <see cref="IOpResult" /> object containing data including whether or /// not the operation was successful and any error messages. /// </returns> public IOpResult UpdateBillingTerm(BillingTerm updatedTerm) { var result = new OpResult(); try { using (var ctx = new TimeTrackerContext()) { // Update the object var existingEntity = ctx.BillingTerms.First(b => b.BillingTermsId == updatedTerm.BillingTermsId); ctx.Entry(existingEntity).CurrentValues.SetValues(updatedTerm); ctx.SaveChanges(); // Return success result.IsSuccessful = true; return(result); } } catch (Exception ex) { // Return failure result.IsSuccessful = false; result.ErrorMessage = ex.Message; return(result); } }
//Normal version doesn't save type or isActive public bool ChangeUser(User user) { var entry = _context.Users.First(u => u.userID == user.userID); _context.Entry(entry).CurrentValues.SetValues(user); _context.SaveChanges(); return(true); }
/// <summary> /// Saves the profile. /// </summary> /// <param name="updatedProfile">The updated profile.</param> /// <returns>OpResult.</returns> public OpResult SaveProfile(UserProfile updatedProfile) { var result = new OpResult(); try { using (var ctx = new TimeTrackerContext()) { // Get the User profile var profile = ctx.UserProfiles.FirstOrDefault(p => p.UserId.Equals(updatedProfile.UserId)); // If there isn't a profile to edit if (profile == null) { // Add the profile ctx.UserProfiles.Add(updatedProfile); ctx.SaveChanges(); } else { // Update the object ctx.Entry(profile).CurrentValues.SetValues(updatedProfile); ctx.SaveChanges(); } // Return success result.IsSuccessful = true; return result; } } catch (Exception ex) { // Return failure result.IsSuccessful = false; result.ErrorMessage = ex.Message; return result; } }