public async Task <IActionResult> Link([FromBody] SensorLink link) { var status = new Response <string>(); var error = false; try { if (link.UserId == this.CurrentUser.Email) { status.AddError("Unable to link sensor to own account!"); return(this.BadRequest(status)); } var sensor = await this.m_sensors.GetAsync(link.SensorId).ConfigureAwait(false); var user = await this.m_accounts.GetAccountByEmailAsync(link.UserId).ConfigureAwait(false); if (!await this.AuthenticateUserForSensor(sensor, false).ConfigureAwait(false)) { return(this.Forbidden()); } if (user == null) { status.AddError("Target user not found."); error = true; } if (sensor == null) { status.AddError("Target sensor not found."); error = true; } if (error) { return(this.BadRequest(status)); } link.UserId = user.ID.ToString(); await this.m_links.CreateAsync(link).ConfigureAwait(false); } catch (PostgresException ex) { if (ex.SqlState != PostgresErrorCodes.UniqueViolation) { return(this.StatusCode(500)); } var response = new Response <string>(); response.AddError("This link already exists!"); return(this.UnprocessableEntity(response)); } catch (FormatException ex) { var response = new Response <string>(); response.AddError(ex.Message); return(this.UnprocessableEntity(response)); } return(this.NoContent()); }
public async Task <IActionResult> DeleteLink([FromBody] SensorLink link) { var myemail = this.CurrentUser.Email; try { var auth = await this.AuthenticateUserForSensor(link.SensorId).ConfigureAwait(false); auth |= myemail == link.UserId; var user = await this.m_accounts.GetAccountByEmailAsync(link.UserId).ConfigureAwait(false); if (user == null) { var response = new Response <string>(); response.AddError("Invalid 'userId'."); return(this.UnprocessableEntity(response)); } if (!auth) { return(this.Forbidden()); } link.UserId = user.ID.ToString(); await this.m_links.DeleteAsync(link).ConfigureAwait(false); } catch (FormatException ex) { var response = new Response <string>(); response.AddError(ex.Message); return(this.UnprocessableEntity(response)); } return(this.NoContent()); }
public void addSensorLink(SensorLink sensorLink) { if (dictSensors.ContainsKey(sensorLink.SensorId)) { dictSensors.Remove(sensorLink.SensorId); } dictSensors.Add(sensorLink.SensorId, sensorLink); }
public async Task <bool> DeleteAsync(SensorLink link, CancellationToken token = default) { using var builder = StoredProcedureBuilder.Create(this.m_netDb.Connection); builder.WithFunction(DeleteSensorLink); builder.WithParameter("sensorid", link.SensorId, NpgsqlDbType.Varchar); builder.WithParameter("userid", Guid.Parse(link.UserId), NpgsqlDbType.Uuid); await using var reader = await builder.ExecuteAsync(token).ConfigureAwait(false); return(reader.HasRows); }
public async Task <IEnumerable <SensorLink> > GetByUserAsync(Guid userId, CancellationToken token = default) { using var builder = StoredProcedureBuilder.Create(this.m_netDb.Connection); builder.WithFunction(SelectLinkByUserID); builder.WithParameter("userid", userId, NpgsqlDbType.Uuid); await using var reader = await builder.ExecuteAsync(token).ConfigureAwait(false); var list = new List <SensorLink>(); while (await reader.ReadAsync(token).ConfigureAwait(false)) { var link = new SensorLink { SensorId = reader.GetString(0), UserId = reader.GetGuid(1).ToString() }; list.Add(link); } return(list); }