public static async Task <ErrorClass> UpdatePerson(UpdateClass update) { if (!await IdExistAsyncRoute(update.PersonId)) { return(new ErrorClass { ErrorCode = ErrorList.ERROR_NON_EXISTENT, Description = $"person that you are trying to update doesn't exist (submitted id : {update.PersonId})" }); } var person = await ManagePersonDapper.GetPerson(update.PersonId); var allowedTypes = new List <string> { "Fname", "Lname", "Gender", "PrivateNumber", "Date", "City", "PhoneNumber", "Image" }; if (!allowedTypes.Contains(update.Column)) { return(new ErrorClass { ErrorCode = ErrorList.ERROR_INVALID_INPUT, Description = $"(case sensitive) Fname, Lname , Gender, PrivateNumber, Date, PhoneNumber, Image(submitted value : {update.Value})" }); } var sql = $"UPDATE persons_tbl SET {update.Column} = '{update.Value}' WHERE PersonId = {update.PersonId}"; switch (update.Column) { case "Fname" or "Lname": if (await NameExists(person.Fname, update.Value)) { return new ErrorClass { ErrorCode = ErrorList.OK, Description = "name already exists" } } ; break; case "gender": if (update.Value is not "0" or "1") { return new ErrorClass { ErrorCode = ErrorList.ERROR_INVALID_INPUT, Description = "gender must be 1 or 0" } } ; break; case "PrivateNumber": var v = PersonVerify.PrivateNumber(update.Value); if (v.ErrorCode != ErrorList.OK) { return(v); } break; case "Date": var d = PersonVerify.Date(Convert.ToDateTime(update.Value)); if (d.ErrorCode != ErrorList.OK) { return(d); } break; case "City": if (Regex.IsMatch(update.Column, "\\d")) { return new ErrorClass { ErrorCode = ErrorList.ERROR_INVALID_INPUT, Description = "city contains numbers" } } ; break; } await ManagePersonDapper.UpdatePersonSingle(sql); return(new ErrorClass { ErrorCode = ErrorList.OK, Description = "User Updated" }); } }
public static async Task <Person> GetPerson(string id) { var person = await ManagePersonDapper.GetPerson(id); return(person); }