public async Task <IActionResult> UpdateAuthor(int id, AuthorPutDTO authorToBeUpdated)
        {
            // Check that model is valid, and also that id in URI matches id in Body.
            if (!ModelState.IsValid ||
                id != authorToBeUpdated.AuthorId)
            {
                return(BadRequest(ModelState));
            }

            // Check that an author by that ID exists.
            Author existingAuthor = await _db.Authors
                                    .Where(a => a.AuthorId == id)
                                    .FirstOrDefaultAsync();

            if (existingAuthor == null)
            {
                return(NotFound());
            }

            // Check if the edited author already exists.
            Author existingAuthorWithSameName = await _db.Authors
                                                .Where(a =>
                                                       a.Praenomen == authorToBeUpdated.Praenomen &&
                                                       a.Nomen == authorToBeUpdated.Nomen &&
                                                       a.Cognomen == authorToBeUpdated.Cognomen)
                                                .FirstOrDefaultAsync();

            if (existingAuthorWithSameName != null)
            {
                return(Conflict(new { message = _errorMessageAuthorExists }));
            }

            try
            {
                int userId = (int)_userCtx.GetId();

                // First update author.
                _db.Entry(existingAuthor).CurrentValues.SetValues(authorToBeUpdated);
                existingAuthor.LastEditedBy   = userId;
                existingAuthor.LastEditedDate = DateTime.Now;
                await _db.SaveChangesAsync();

                // Prepare response.
                AuthorGetDTO editedAuthorResponse = AuthorGetDTO.FromModel(existingAuthor);

                // Trigger webhook for edited author event.
                TriggerAuthorWebhook(Event.EditedAuthor, editedAuthorResponse, userId);

                return(Ok(editedAuthorResponse));
            }
            catch (DbUpdateException)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError,
                                  _errorMessageSavingData));
            }
        }
Esempio n. 2
0
 public ActionResult Edit([Bind(Include = "Id,Descripcion,Estado")] UnidadMedida unidadMedida)
 {
     if (ModelState.IsValid)
     {
         db.Entry(unidadMedida).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(unidadMedida));
 }
        public async Task <IActionResult> Edit(string id, [Bind("Id,Role_Name,User_Id,First_Name,Last_Name,Email,Address,Created_At,Updated_At")] User_RoleModel user_RoleModel, string Role_Name)
        {
            if (id != user_RoleModel.User_Id)
            {
                return(NotFound());
            }

            /* To return the selected application user and application role from the database if it exists*/
            var applicationuser = await _context.Users.SingleOrDefaultAsync(x => x.Id == id);

            var new_application_role = await _context.Roles.SingleOrDefaultAsync(r => r.Id == Role_Name);

            var old_application_role = await _context.UserRoles.SingleOrDefaultAsync(ru => ru.UserId == applicationuser.Id);

            var old_RoleName = await _context.Roles.SingleOrDefaultAsync(r => r.Id == old_application_role.RoleId);

            if (ModelState.IsValid)
            {
                try
                {
                    applicationuser.FirstName = user_RoleModel.First_Name;
                    applicationuser.LastName  = user_RoleModel.Last_Name;
                    applicationuser.Email     = user_RoleModel.Email;
                    applicationuser.Address   = user_RoleModel.Address;

                    // To set the system time for record update
                    applicationuser.Updated_At = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Trim());
                    _context.Update(applicationuser);
                    _context.Entry(applicationuser).Property(x => x.Created_At).IsModified = false; // To prevent the datetime property to be set as null on update operation

                    // To update the role of the user by deleting the old role and assigning new role to the user
                    if (old_application_role.RoleId != Role_Name)
                    {
                        await _userManager.RemoveFromRoleAsync(applicationuser, old_RoleName.Name);

                        await _userManager.AddToRoleAsync(applicationuser, new_application_role.Name);
                    }
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ApplicationUserExists(applicationuser.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(AdminIndex)).WithSuccess("Success", "Successfully Updated User Details"));
            }
            return(View(user_RoleModel));
        }
Esempio n. 4
0
        public ActionResult ValidateSMSKeySubmit(PNCModel model)
        {
            using (Data.ApplicationDbContext context = new Data.ApplicationDbContext())
            {
                var result = context.PNC.Where(x => x.VerifcationKey == model.VerifcationKey).FirstOrDefault(); //One Result
                if (result.SMSKey == model.SMSKey)
                {
                    result.IsValid = true;
                    context.Entry(result).State = EntityState.Modified;
                    context.SaveChanges();

                    ViewBag.Message = "You have been verified!";
                }
            }



            return(View("ValidateSMSKey", model));
        }
Esempio n. 5
0
        public virtual async Task <TEntity> UpdateAsync(TEntity resource, object key)
        {
            if (resource == null)
            {
                return(null);
            }

            var exist = await DbSet.FindAsync(key);

            if (exist == null)
            {
                return(null);
            }

            Context.Entry((object)exist).CurrentValues.SetValues(resource);

            await SaveAsync();

            return(exist);
        }
Esempio n. 6
0
 public TaskItem PutTask(int id, [FromBody] Todoapp.Models.TaskItem task)
 {
     _context.Entry(task).State = EntityState.Modified;
     _context.SaveChanges();
     return(task);
 }
Esempio n. 7
0
 public void Update(Learner learner)
 {
     _context.Entry(learner).State = EntityState.Modified;
     _context.SaveChanges();
 }
Esempio n. 8
0
        public async Task <IActionResult> PutStudent([FromRoute] int id, [FromBody] Student Student)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != Student.Id)
            {
                return(BadRequest());
            }

            var user = await GetCurrentUserAsync();

            var userId = user?.Id;

            Student.IdentityId = userId;
            if (Student.FirstName == null)
            {
                Student st = _context.Student.Where(p => p.Id == id).FirstOrDefault();

                st.IsApproved = Student.IsApproved;

                _context.Entry(st).State = EntityState.Modified;
            }

            else

            {
                //Student st = _context.Student.Where(p => p.Id == id).FirstOrDefault();

                //Student.IsApproved = st.IsApproved;
                session.SetString("image", Student.Profile);

                //create new city
                //if (Student.CityId == null && Student.City != null)
                //{
                //    City objNewCity = new City();
                //    objNewCity.Name = Student.City.Name;
                //    objNewCity.StateId = Student.City.StateId;
                //    _context.City.Add(objNewCity);
                //    _context.SaveChanges();
                //    var cityList = _context.City.OrderByDescending(c => c.Id).Take(1).FirstOrDefault();
                //    Student.CityId = cityList.Id;

                //}
                _context.Entry(Student).State = EntityState.Modified;
            }


            try
            {
                await _context.SaveChangesAsync();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(Ok(0));
            }
            return(Ok(Student));
            // return CreatedAtAction("GetStudent", new { id = Student.Id }, Student);
        }
        public async Task <IActionResult> UpdateWork(int id, WorkPutDTO workToBeUpdated)
        {
            // Check that model is valid, and also that id in URI matches id in Body.
            if (!ModelState.IsValid ||
                id != workToBeUpdated.WorkId)
            {
                return(BadRequest(ModelState));
            }

            // Check that work by that ID exists.
            Work existingWork = await _db.Works
                                .Where(w => w.WorkId == id)
                                .FirstOrDefaultAsync();

            if (existingWork == null)
            {
                return(NotFound());
            }

            // Check if author exists.
            Author existingAuthor = await _db.Authors
                                    .FirstOrDefaultAsync(a => a.AuthorId == workToBeUpdated.AuthorId);

            if (existingAuthor == null)
            {
                return(BadRequest(new { message = _errorMessageAuthorNotExists }));
            }

            // Check if work with that title already exists.
            Work existingWorkWithTitle = await _db.Works
                                         .FirstOrDefaultAsync(w => w.Title == workToBeUpdated.Title);

            if (existingWorkWithTitle != null)
            {
                return(Conflict(new { message = _errorMessageWorkExists }));
            }

            try
            {
                int userId = (int)_userCtx.GetId();

                // First update work.
                _db.Entry(existingWork).CurrentValues.SetValues(workToBeUpdated);
                existingWork.LastEditedBy   = userId;
                existingWork.LastEditedDate = DateTime.Now;
                await _db.SaveChangesAsync();

                // Prepare response.
                WorkGetDTO editedWorkResponse = WorkGetDTO.FromModel(existingWork);

                // Trigger webhook for edited work.
                TriggerWorkWebhook(Event.EditedWork, editedWorkResponse, userId);

                return(Ok(editedWorkResponse));
            }
            catch (DbUpdateException)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError,
                                  _errorMessageSavingData));
            }
        }
 public void Update(Activity activity)
 {
     _context.Entry(activity).State = EntityState.Modified;
     _context.SaveChanges();
 }
Esempio n. 11
0
 public void Update(Behavior behavior)
 {
     _context.Entry(behavior).State = EntityState.Modified;
     _context.SaveChanges();
 }
Esempio n. 12
0
 public Project PutProject(int id, [FromBody] Todoapp.Models.Project project)
 {
     _context.Entry(project).State = EntityState.Modified;
     _context.SaveChanges();
     return(project);
 }
Esempio n. 13
0
 public void Update(Report report)
 {
     _context.Entry(report).State = EntityState.Modified;
     _context.SaveChanges();
 }