Skip to content

Commit

Permalink
implement update and refactor error handling in insert
Browse files Browse the repository at this point in the history
  • Loading branch information
DenverCoder544 committed Oct 3, 2023
1 parent eaaed62 commit 1079f6d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public void handlePut(ActionParameters params) throws ActionException {

try {
LOG.debug("Updating MyPlaces:", ids);
int updated = featureService.update(places);
int updated = mybatisFeatureService.update(places);
LOG.info("Updated", updated, "/", places.size());
} catch (ServiceException e) {
LOG.warn(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,16 @@ public interface MyPlaceMapper {
@Options(useGeneratedKeys=true, keyColumn="id", keyProperty="id")
Long addMyPlace(MyPlace myPlace);

@Update("UPDATE my_places SET " +
" category_id = #{categoryId}, " +
" name = #{name}, " +
" attention_text = #{attentionText}, " +
" updated = now(), " +
" geometry = ST_SetSRID(ST_GeometryFromText(#{geomAsText}), #{srid}), " +
" place_desc = #{desc}, " +
" link = #{link}, " +
" image_url = #{imageUrl} " +
" WHERE "+
" id = #{id} ")
Long updateMyPlace(MyPlace myPlace);
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,37 @@ public long[] insert(List<MyPlace> places) throws ServiceException {
LOG.info("inserted myplace: ", place.getId());
}
session.commit();
return places.stream().mapToLong(MyPlace::getId).toArray();
} catch (Exception e) {
LOG.warn(e, "Exception when trying to add MyPlaces: ");
throw new ServiceException(e.getMessage());
}
}


long[] ids = places.stream().mapToLong(place -> place.getId()).toArray();
return ids;
@Override
public int update(List<MyPlace> places) throws ServiceException {
try (SqlSession session = factory.openSession()) {
LOG.debug("Adding new places: ", places);
final MyPlaceMapper mapper = session.getMapper(MyPlaceMapper.class);
for (MyPlace place : places) {
Geometry transformed = this.doGeometryTransform(place.getGeometry());
place.setGeometry(transformed);

mapper.updateMyPlace(place);
LOG.info("inserted myplace: ", place.getId());
}
session.commit();
return places.size();
} catch (Exception e) {
LOG.warn(e, "Exception when trying to add MyPlaces ");
throw new ServiceException(e.getMessage());
}
}

@Override
public int delete(long[] ids) throws ServiceException {
return 0;
}

private Geometry doGeometryTransform(Geometry geometry) throws ServiceException {
Expand Down Expand Up @@ -124,13 +149,5 @@ private int getSRID(String srsName) {
return Integer.parseInt(srid);
}

@Override
public int update(List<MyPlace> places) throws ServiceException {
return 0;
}

@Override
public int delete(long[] ids) throws ServiceException {
return 0;
}
}

0 comments on commit 1079f6d

Please sign in to comment.