コード例 #1
0
        public async Task <IActionResult> PostRecordAsync([FromBody] PatientModel request)
        {
            Logger?.LogDebug("'{0}' has been invoked", nameof(PostRecordAsync));

            var response = new SingleResponse <PatientModel>();

            try
            {
                var existingEntity = await DbContext
                                     .GetPatientDetailByNameAsync(new Patient { Forename = request.Forename, Surname = request.Surname });

                if (existingEntity != null)
                {
                    ModelState.AddModelError("PatientModel", "Patient name already exists");
                }

                if (!ModelState.IsValid)
                {
                    return(BadRequest());
                }

                Patient entity = new Patient
                {
                    Forename        = request.Forename,
                    Surname         = request.Surname,
                    Gender          = request.Gender,
                    DateOfBirth     = request.DateOfBirth,
                    TelephoneNumber = request.TelephoneNumber != null?JsonConvert.SerializeObject(request.TelephoneNumber) : null,
                                          CreatedOn = DateTime.UtcNow,
                                          CreatedBy = "System"
                };

                // Add entity to repository
                DbContext.Add(entity);

                // Save entity in database
                await DbContext.SaveChangesAsync();

                // Set the entity to response model
                response.Model = request;
            }
            catch (Exception ex)
            {
                response.DidError     = true;
                response.ErrorMessage = "There was an internal error, please contact to support.";

                Logger?.LogCritical("There was an error on '{0}' invocation: {1}", nameof(PostRecordAsync), ex);
            }

            return(response.ToHttpResponse());
        }