Próbuję dokończyć aplikację i właściwie został mi ostatni problem do rozwiązania, a mianowicie chciałbym zapisywać pewne zgłoszenie w bazie danych. Aplikacja łączy się z bazą przez API, gdzie encja która będzie zapisywana wygląda tak:
public class DefectsEntity
{
[Key]
public int idDefect { get; set; }
public string Number_notification { get; set; }
public int IdRoad { get; set; }
public int IdElement { get; set; }
public int IdIntensity { get; set; }
public string Description { get; set; }
public string Recommendation { get; set; }
public int IdUser { get; set; }
public DateTime Notification_date { get; set; }
public string IdPhoto { get; set; }
public decimal Latitude { get; set; }
public decimal Longititude { get; set; }
public virtual ElementsEntity Elements { get; set; }
public virtual RoadsEntity Roads { get; set; }
public virtual IntensityEntity Intensity { get; set; }
public virtual UsersEntity Users { get; set; }
public DefectsEntity()
{
}
public DefectsEntity(int idRoad, int idElement, int idIntensity, string description, string recommendation, int idUser, DateTime notification_date, string idPhoto, decimal latitude, decimal longititude,
ElementsEntity element, RoadsEntity roads, IntensityEntity intensity, UsersEntity user)
{
IdRoad = idRoad;
IdElement = idElement;
IdIntensity = idIntensity;
Description = description;
Recommendation = recommendation;
IdUser = idUser;
Notification_date = notification_date;
IdPhoto = idPhoto;
Latitude = latitude;
Longititude = longititude;
Elements = element;
Roads = roads;
Intensity = intensity;
Users = user;
}
}
W bazie danych dwie pierwsze kolumny (idDefect oraz Number_Notification) uzupełniane są automatycznie. Kontroler który obsługuje dodanie zgłoszenia:
[HttpPost]
public IActionResult Create([FromBody]DefectsEntity defect)
{
try
{
if (defect == null || !ModelState.IsValid)
{
return BadRequest(ErrorCode.TodoItemNameAndNotesRequired.ToString());
}
bool defectExists = _defectRepository.DoesItemExist(defect.idDefect);
if (defectExists)
{
return StatusCode(StatusCodes.Status409Conflict, ErrorCode.TodoItemIDInUse.ToString());
}
_defectRepository.Add(defect);
_defectRepository.Save();
}
catch (Exception)
{
return BadRequest(ErrorCode.CouldNotCreateItem.ToString());
}
return Ok(defect);
}
Po stronie aplikacji metoda komunikująca się z API w celu dodania zgłoszenia:
public async Task SaveDefectAsync(DefectEntity defect)
{
Uri uri = new Uri(string.Format(Constants.DefectsRestUrl, string.Empty));
try
{
string json = JsonSerializer.Serialize<DefectEntity>(defect, serializerOptions);
StringContent content = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage response = null;
response = await client.PostAsync(uri, content);
if (response.IsSuccessStatusCode)
{
Debug.WriteLine(@"\tDefect successfully saved.");
}
else
{
Debug.WriteLine(@"\tDefect failure");
}
}
catch (Exception ex)
{
Debug.WriteLine(@"\tERROR {0}", ex.Message);
}
}
JSON który idzie z aplikacji do API:
{
"idDefect": 0,
"number_notification": null,
"idRoad": 4,
"idElement": 6,
"idIntensity": 3,
"description": "Gggf",
"recommendation": "Xvfff",
"idUser": 2,
"notification_date": "2021-05-21T14:57:55.4151+02:00",
"idPhoto": "995d7418-90cb-48b2-8784-4ed978c0981d.jpg",
"latitude": 52.45641191,
"longititude": 20.95981319,
"roads": {
"idRoad": 4,
"number_road": "G877326P",
"name_road": "Franciszka\u0144ska"
},
"elements": {
"idElement": 6,
"name_Element": "Wpust deszczowy"
},
"intensity": {
"idIntensity": 3,
"name_Intensity": "Du\u017Ca"
},
"users": {
"idUser": 2,
"nameUser": "Pawe\u0142",
"surnameUser": "Mickiewicz",
"email": "p_mickiewicz@testdomain.onmicrosoft.com"
}
}
No i element nie dodaje się do bazy danych, zwracany jest błąd 400. Próbowałem z Postman ale skutek ten sam czyli podejrzewam że mam błąd po stronie API lub źle tworzę JSON. Dodawanie danych do innych tabel działa bez zarzutu, jednak ta jest bardziej złożona bo posiada klucze obce.