public async Task <Member> CreateMember(CreateMemberCommand command) { if (command == null) { throw new CustomBadRequestException(ExceptionMessage.NullCommand); } if (String.IsNullOrEmpty(command.PhoneNumber) && String.IsNullOrEmpty(command.Email)) { throw new CustomBadRequestException(ExceptionMessage.MemberEmptyEmailAndPhone); } if (command.IdCard == null) { throw new CustomBadRequestException(ExceptionMessage.IdCardNull); } if (!String.IsNullOrEmpty(command.Email)) { if (!IsEmailValid(command.Email)) { throw new CustomBadRequestException(ExceptionMessage.BadEmailFormat); } } var idCard = new IdentityCard { Firstname = command.IdCard.Firstname, Lastname = command.IdCard.Lastname, BirthDate = command.IdCard.BirthDate, CardNumber = command.IdCard.CardNumber, ExpirationDate = command.IdCard.ExpirationDate, ValidityDate = command.IdCard.ValidityDate, NationalRegisterNumber = command.IdCard.NationalRegisterNumber }; ValidateIdCard(idCard); var memberCard = MemberCard.GenerateMemberCard(); var member = new Member { Email = command.Email, PhoneNumber = command.PhoneNumber, IdentityCards = new List <IdentityCard>() { idCard }, MemberCards = new List <MemberCard>() { memberCard }, IsActive = true }; _context.Members.Add(member); await _context.SaveChangesAsync(); return(member); }
public async Task <Member> GenerateNewMemberCard(int memberId) { var member = _context.Members .Include(m => m.MemberCards) .SingleOrDefault(m => m.Id == memberId); if (member == null) { throw new CustomNotFoundException(ExceptionMessage.MemberIdNotFound); } foreach (var memberCard in member.MemberCards) { if (memberCard.IsActive) { memberCard.IsActive = false; } } member.MemberCards.Add(MemberCard.GenerateMemberCard()); await _context.SaveChangesAsync(); return(member); }