File tree Expand file tree Collapse file tree 1 file changed +11
-13
lines changed
src/main/java/ru/shortener/service Expand file tree Collapse file tree 1 file changed +11
-13
lines changed Original file line number Diff line number Diff line change 22
33import org .springframework .beans .factory .annotation .Autowired ;
44import org .springframework .stereotype .Component ;
5- import org .springframework .util .Assert ;
65import ru .shortener .model .Link ;
6+ import ru .shortener .repository .LinkRepository ;
77
8- import java .util .HashMap ;
9- import java .util .Map ;
10- import java .util .concurrent .atomic .AtomicLong ;
8+ import java .util .Optional ;
119
1210@ Component
1311public class DefaultKeyMapperService implements KeyMapperService {
1412
15- private Map <Long , Link > map = new HashMap <>();
16-
1713 @ Autowired
18- KeyConverterService converterService ;
14+ private KeyConverterService converterService ;
1915
20- private AtomicLong sequence = new AtomicLong (10000000L );
16+ @ Autowired
17+ private LinkRepository repository ;
2118
2219 @ Override
2320 public String add (String url ) {
24- Long id = sequence .getAndIncrement ();
2521 Link link = new Link ();
2622 link .setUrl (url );
27- map .put (id , link );
28- return converterService .idToKey (id );
23+ return converterService .idToKey (repository .save (link ).getId ());
2924 }
3025
3126 @ Override
3227 public Link getLink (String key ) {
3328 Long id = converterService .keyToId (key );
34- Assert .notNull (map .get (id ), "Link with key non exists: " + key );
35- return map .get (id );
29+ Optional <Link > res = repository .findOne (id );
30+ if (!res .isPresent ()) {
31+ throw new RuntimeException ("Link with key non exists: " + key );
32+ }
33+ return res .get ();
3634 }
3735}
You can’t perform that action at this time.
0 commit comments