public async Task <IActionResult> Post([FromBody] TeamDTO teamDTO)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            Organization org = await _context.Organizations.Where(o => o.Name == teamDTO.OrganizationName).FirstOrDefaultAsync();

            if (!String.IsNullOrWhiteSpace(teamDTO.OrganizationName) && org == null)
            {
                return(BadRequest("Specified organization does not exist"));
            }

            var team = new Team {
                CreatedTs    = DateTime.Now,
                Postcode     = teamDTO.Postcode,
                Address      = teamDTO.Address,
                TeamName     = teamDTO.TeamName,
                Description  = teamDTO.Description,
                FacebookUrl  = teamDTO.FacebookUrl,
                Organization = org
            };

            await _context.AddAsync(team);

            await _context.SaveChangesAsync();

            return(Ok(team.Id));
        }
        public async Task <IActionResult> Post([FromBody] VolunteerDTO volunteerDTO)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            Organization org = await _context.Organizations.Where(o => o.Name == volunteerDTO.OrganizationName).FirstOrDefaultAsync();

            if (!string.IsNullOrWhiteSpace(volunteerDTO.OrganizationName) && org == null)
            {
                return(BadRequest("Specified organization does not exist"));
            }

            var identification = new Identification {
                LastUpdatedTs = DateTime.Now,
                Active        = true
            };

            await _context.AddAsync(identification); // Create Identification

            var volunteer = new Volunteer {
                CreatedTs      = DateTime.Now,
                AuthToken      = volunteerDTO.AuthToken,
                FirstName      = volunteerDTO.FirstName,
                LastName       = volunteerDTO.LastName,
                Postcode       = volunteerDTO.Postcode,
                Phonenumber    = volunteerDTO.Phonenumber,
                Email          = volunteerDTO.Email,
                VolunteerType  = volunteerDTO.VolunteerType,
                Organization   = org,
                Identification = identification
            };

            await _context.AddAsync(volunteer); // Create Volunteer

            using (SHA256 sha256Hash = SHA256.Create()) {
                string stringToBeHashed = $"{identification.Id}{volunteerDTO.LastName}{volunteerDTO.Phonenumber}{volunteerDTO.Email}{volunteer.Id}";
                byte[] bytes            = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(stringToBeHashed));

                StringBuilder builder = new StringBuilder();
                for (int i = 0; i < bytes.Length; i++)
                {
                    builder.Append(bytes[i].ToString("x2"));
                }

                identification.UniqueToken = builder.ToString();
                identification.Volunteer   = volunteer; // Tie Volunteer to Identification
                //_context.Update(identification); //TODO Is this necessary?
            }

            await _context.SaveChangesAsync();

            return(Ok(volunteer.Id));
        }
Example #3
0
        public async Task <IActionResult> Post([FromBody] OrganizationDTO organizationDTO)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var org = await _context.Organizations.FindAsync(organizationDTO.Name);

            if (org != null)
            {
                return(BadRequest("Organization with the specified name already exists."));
            }

            var organization = new Organization {
                CreatedTs = DateTime.Now,
                Name      = organizationDTO.Name
            };

            await _context.AddAsync(organization);

            await _context.SaveChangesAsync();

            return(Json(organization));
        }