public async Task <long> SystemInsert(PersonInsertOrUpdateDto dto) { var entity = dto.ToEntity(); _context.People.Add(entity); await _context.SaveChangesAsync(); return(entity.Id); }
public async Task <long> Insert(string contextUserEmail, PersonInsertOrUpdateDto dto) { await _userService.ThrowIfNotInRole(contextUserEmail, UserRoleEnum.Editor); if (dto == null) { throw new ArgumentNullException(nameof(PersonInsertOrUpdateDto)); } return(await SystemInsert(dto)); }
public async Task <long> SystemEnsurePersonExists(PersonInsertOrUpdateDto personDto) { //This code should run in a transaction at the controller level, since we 'find' and then possibly 'insert' //This method uses the email address and person's name to identify them. //NOTE: This is an area where we may get duplicates. For instance, someone may have different first names (i.e. nicknames, shortened names) // that they go by. var person = await Find(p => personDto.Email == p.Email && personDto.FullName == p.FullName); if (person == null) { return(await SystemInsert(personDto)); } return(person.Id); }
public async Task <IActionResult> Put(long id, [FromBody] PersonInsertOrUpdateDto dto) { var trans = await _personService.BeginTransactionAsync(); try { await _personService.Update(ContextUserEmail, dto, id); trans.Commit(); var data = await _personService.Find(x => x.Id == id); return(Ok(data)); } catch (Exception e) { trans.Rollback(); return(Exception(e)); } }
public async Task Update(string contextUserEmail, PersonInsertOrUpdateDto dto, long personId) { await _userService.ThrowIfNotInRole(contextUserEmail, UserRoleEnum.Editor); if (dto == null) { throw new ArgumentNullException(nameof(PersonInsertOrUpdateDto)); } var entity = await _context.People.FirstOrDefaultAsync(p => p.Id == personId); if (entity == null) { throw new EntityNotFoundException(personId); } entity.FullName = dto.FullName; entity.Email = dto.Email; entity.MembershipDate = dto.MembershipDate; entity.FirstVisitDate = dto.FirstVisitDate; entity.ContactNumber = dto.ContactNumber; entity.Description = dto.Description; entity.Status = dto.Status; entity.AgeGroup = dto.AgeGroup; if (!entity.FirstVisitDate.HasValue && entity.Status == PersonStatusEnum.Visitor) { entity.FirstVisitDate = DateTimeOffset.UtcNow; } if (!entity.MembershipDate.HasValue && entity.Status == PersonStatusEnum.Member) { entity.MembershipDate = DateTimeOffset.UtcNow; } await _context.SaveChangesAsync(); }
public async Task <IActionResult> NextQuestion(long quizId, [FromBody] PersonInsertOrUpdateDto personDto, [FromBody] QuizQuestionAnswerDto answersToPreviousQuestion) { var trans = await _personService.BeginTransactionAsync(); try { //Need to determine if this person already exists in the database, and if not add him. var personId = await _personService.SystemEnsurePersonExists(personDto); var answerResponse = await _quizService.AnswerQuestion(quizId, personId, answersToPreviousQuestion); trans.Commit(); var data = await _quizService.NextQuestion(quizId, personId, answersToPreviousQuestion); return(Ok(data)); } catch (Exception e) { trans.Rollback(); return(Exception(e)); } }