Beispiel #1
0
        public async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "put", Route = "PutUser/{Id}")] Common.Models.UserProfile userProfile, HttpRequest req, string Id, ILogger log)
        {
            log.LogInformation("PUT - Put User requested");
            try
            {
                if (!RIPAAuthorization.ValidateUserOrAdministratorRole(req, log).ConfigureAwait(false).GetAwaiter().GetResult())
                {
                    return(new UnauthorizedResult());
                }
            }
            catch (Exception ex)
            {
                log.LogError(ex.Message);
                return(new UnauthorizedResult());
            }

            if (!string.IsNullOrEmpty(userProfile.OfficerId) && userProfile.OfficerId.Length != 9)
            {
                return(new BadRequestObjectResult("OfficerId must be 9 chars"));
            }

            if (string.IsNullOrEmpty(userProfile.OfficerId))
            {
                int officerId = 100000000;

                string query = "SELECT VALUE c FROM c ORDER BY c.officerId DESC OFFSET 0 LIMIT 1";
                IEnumerable <Common.Models.UserProfile> maxOfficer = await _userProfileCosmosDbService.GetUserProfilesAsync(query);

                Common.Models.UserProfile maxId = maxOfficer.FirstOrDefault();
                if (maxId != null)
                {
                    officerId = int.Parse(maxId.OfficerId);
                    officerId++;
                }

                userProfile.OfficerId = officerId.ToString();
            }

            userProfile.Id = Id;
            await _userProfileCosmosDbService.UpdateUserProfileAsync(Id, userProfile);

            return(new OkObjectResult(userProfile));
        }
Beispiel #2
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("Importing user profiles from uploaded csv");
            var count = 0;

            try
            {
                if (!RIPAAuthorization.ValidateAdministratorRole(req, log).ConfigureAwait(false).GetAwaiter().GetResult())
                {
                    return(new UnauthorizedResult());
                }
            }
            catch (Exception ex)
            {
                log.LogError(ex.Message);
                return(new UnauthorizedResult());
            }

            try
            {
                using (var reader = new StreamReader(req.Form.Files[0].OpenReadStream()))
                    using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
                    {
                        var agency = req.Query["agency"];
                        csv.Context.RegisterClassMap <UserProfileMap>();
                        var records = csv.GetRecords <Common.Models.UserProfile>().ToList();
                        count = records.Count();

                        foreach (var record in records)
                        {
                            record.StartDate = DateTime.Now.AddYears(-record.YearsExperience);
                            if (string.IsNullOrEmpty(record.Agency))
                            {
                                record.Agency = agency;
                            }
                            record.FirstName ??= "";
                            record.LastName ??= "";
                            await _userProfileCosmosDbService.UpdateUserProfileAsync(record.Id, record);
                        }
                    }

                string responseMessage;

                if (count >= 0)
                {
                    responseMessage = $"Upload Complete: {count} {(count > 1 ? "records" : "record")} uploaded";
                }
                else
                {
                    responseMessage = "No records found";
                }

                return(new OkObjectResult(responseMessage));
            }
            catch (Exception ex)
            {
                log.LogError(ex.Message);
                return(new BadRequestObjectResult("There was an error with the file format.  Please verify data and try again."));
            }
        }