public async Task <ActionResult <TypeOfExamDto> > PutTypeOfExam(int id, TypeOfExamInputDto input) { var typeOfExam = await _context.TypeOfExams.FindAsync(id); typeOfExam.Name = input.Name; typeOfExam.ForeignName = input.ForeignName; typeOfExam.Cost = input.Cost; typeOfExam.UpdatedUserId = input.UserId; typeOfExam.UpdatedDate = DateTime.Now; _context.Entry(typeOfExam).State = EntityState.Modified; var user = await _context.Users.FirstOrDefaultAsync(x => x.Id == input.UserId); var log = new Log() { DateTime = DateTime.Now, TypeFullName = typeof(TypeOfExam).FullName, Content = "@userName@updateAction@objTitle", TypeId = typeOfExam.Id, UserId = user.Id }; _context.Logs.Add(log); try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!TypeOfExamExists(id)) { return(NotFound()); } else { throw; } } var result = new TypeOfExamDto() { Id = typeOfExam.Id, Name = typeOfExam.Name, ForeignName = typeOfExam.ForeignName, Cost = typeOfExam.Cost }; return(result); }
public async Task <ActionResult <TypeOfExamDto> > PostTypeOfExam(TypeOfExamInputDto input) { try { var typeOfExam = new TypeOfExam() { Name = input.Name, ForeignName = input.ForeignName, Cost = input.Cost, CreatedDate = DateTime.Now, CreatedUserId = input.UserId }; _context.TypeOfExams.Add(typeOfExam); await _context.SaveChangesAsync(); var user = await _context.Users.FirstOrDefaultAsync(x => x.Id == input.UserId); var log = new Log() { DateTime = DateTime.Now, TypeFullName = typeof(TypeOfExam).FullName, Content = "@userName@addAction@objTitle", TypeId = typeOfExam.Id, UserId = user.Id }; _context.Logs.Add(log); await _context.SaveChangesAsync(); var result = new TypeOfExamDto() { Id = typeOfExam.Id, Name = typeOfExam.Name, ForeignName = typeOfExam.ForeignName, Cost = typeOfExam.Cost }; return(result); } catch (Exception ex) { return(null); } }