Esempio n. 1
0
        public object Patch(EmployeePatch dto)
        {
            // partial updates

            // get from persistent data store by id from routing path
            var emp = Repository.GetById(dto.Id);

            if (emp != null)
            {
                // update values which are specified to update only
                emp.populateFromJsonPatch(dto);

                // update
                Repository.Store(emp);
            }

            // return HTTP Code and Location: header for the new resource
            // 204 No Content; The request was processed successfully, but no response body is needed.
            return(new HttpResult()
            {
                StatusCode = HttpStatusCode.NoContent,
                Location = base.Request.AbsoluteUri,
                Headers =
                {
                    // allow jquery ajax in firefox to read the Location header - CORS
                    { "Access-Control-Expose-Headers", "Location" },
                }
            });
        }
        /// <summary>
        /// Updates a single Employee with one or more values
        /// </summary>
        /// <param name="updatedEmployee">The new data for the Employee you wish to update</param>
        /// <returns>Returns a result indicating if the operation succeeded</returns>
        public async Task <Result> UpdateEmployee(EmployeePatch updatedEmployee)
        {
            try
            {
                using (var con = new Npgsql.NpgsqlConnection(settings.Connection.DatabaseConnectionString))
                {
                    var sqlPatchOperations = new StringBuilder();
                    var obj            = updatedEmployee;
                    var operationCount = 0;

                    if (obj.EmployeeName != null)
                    {
                        sqlPatchOperations.AppendLine(obj.EmployeeName.Operation == OperationKind.Remove
                            ? "employeeName = NULL,"
                            : "employeeName = @EmployeeName,"
                                                      );
                        operationCount++;
                    }
                    if (obj.Username != null)
                    {
                        sqlPatchOperations.AppendLine(obj.Username.Operation == OperationKind.Remove
                            ? "username = NULL,"
                            : "username = @Username,"
                                                      );
                        operationCount++;
                    }
                    if (obj.PasswordHash != null)
                    {
                        sqlPatchOperations.AppendLine(obj.PasswordHash.Operation == OperationKind.Remove
                            ? "passwordHash = NULL,"
                            : "passwordHash = @PasswordHash,"
                                                      );
                        operationCount++;
                    }
                    if (obj.CompanyId != null)
                    {
                        sqlPatchOperations.AppendLine(obj.CompanyId.Operation == OperationKind.Remove
                            ? "companyId = NULL,"
                            : "companyId = @CompanyId,"
                                                      );
                        operationCount++;
                    }
                    if (obj.VenueId != null)
                    {
                        sqlPatchOperations.AppendLine(obj.VenueId.Operation == OperationKind.Remove
                            ? "venueId = NULL,"
                            : "venueId = @VenueId,"
                                                      );
                        operationCount++;
                    }
                    if (obj.RoleId != null)
                    {
                        sqlPatchOperations.AppendLine(obj.RoleId.Operation == OperationKind.Remove
                            ? "roleId = NULL,"
                            : "roleId = @RoleId,"
                                                      );
                        operationCount++;
                    }
                    if (obj.SecurityStamp != null)
                    {
                        sqlPatchOperations.AppendLine(obj.SecurityStamp.Operation == OperationKind.Remove
                            ? "securityStamp = NULL,"
                            : "securityStamp = @SecurityStamp,"
                                                      );
                        operationCount++;
                    }
                    if (obj.Confirmed != null)
                    {
                        sqlPatchOperations.AppendLine(obj.Confirmed.Operation == OperationKind.Remove
                            ? "confirmed = NULL,"
                            : "confirmed = @Confirmed,"
                                                      );
                        operationCount++;
                    }

                    var patchOperations = sqlPatchOperations.ToString();

                    if (operationCount > 0)
                    {
                        // Remove final ", " from StringBuilder to ensure query is valid
                        patchOperations = patchOperations.TrimEnd(System.Environment.NewLine.ToCharArray());
                        patchOperations = patchOperations.TrimEnd(',');
                    }

                    await con.ExecuteAsync($"UPDATE \"Employee\" SET {patchOperations} WHERE employeeId = @ResourceId", new {
                        ResourceId    = obj.ResourceId,
                        EmployeeName  = (string)(obj.EmployeeName == default ? default : obj.EmployeeName.Value),
                        Username      = (string)(obj.Username == default ? default : obj.Username.Value),
                        PasswordHash  = (string)(obj.PasswordHash == default ? default : obj.PasswordHash.Value),
                        CompanyId     = (int)(obj.CompanyId == default ? default : obj.CompanyId.Value),
                        VenueId       = (int)(obj.VenueId == default ? default : obj.VenueId.Value),
                        RoleId        = (int)(obj.RoleId == default ? default : obj.RoleId.Value),
                        SecurityStamp = (string)(obj.SecurityStamp == default ? default : obj.SecurityStamp.Value),
                        Confirmed     = (bool)(obj.Confirmed == default ? default : obj.Confirmed.Value)
                    }).ConfigureAwait(false);

                    return(Result.Ok());
                }