99import com .lzq .api .service .ExampleService ;
1010import com .lzq .api .service .FavoritesService ;
1111import com .lzq .web .utils .ExampleUtils ;
12+ import com .lzq .web .utils .JWTUtils ;
1213import com .lzq .web .utils .QiniuyunUtils ;
1314import com .lzq .web .utils .ResultMapUtils ;
1415import io .swagger .annotations .Api ;
2627import org .springframework .data .redis .core .RedisTemplate ;
2728import org .springframework .web .bind .annotation .*;
2829
30+ import javax .servlet .http .HttpServletRequest ;
2931import java .io .File ;
3032import java .io .FileNotFoundException ;
3133import java .io .FileOutputStream ;
@@ -73,7 +75,7 @@ public class ExampleController {
7375 @ PostMapping ("/createExample" )
7476 @ ApiOperation ("创建一个实例" )
7577 public Map <String , Object > CreateFile (Example example , Content exampleContent , String content ) {
76- log .info ("开始" +System .currentTimeMillis ());
78+ log .info ("开始" +new Date ( System .currentTimeMillis ()). toString ());
7779 //生成22位uuid
7880 String uuid = ExampleUtils .getUUid ();
7981 Boolean bol =false ;
@@ -88,6 +90,7 @@ public Map<String, Object> CreateFile(Example example, Content exampleContent, S
8890 example .setImg (query .getImg ());
8991 //用户保存实例
9092 bol = ExampleUtils .SaveExampleContent (example , exampleContent , content , exampleService , contentService );
93+ log .info ("结束" +new Date (System .currentTimeMillis ()).toString ());
9194 return ResultMapUtils .ResultMap (bol ,0 ,"保存实例内容成功" );
9295 } catch (IOException e ) {
9396 e .printStackTrace ();
@@ -120,6 +123,7 @@ public Map<String, Object> CreateFile(Example example, Content exampleContent, S
120123 accountService .addWorks (example .getUsername ());
121124 //保存实例内容
122125 bol = ExampleUtils .SaveExampleContent (example , exampleContent , content , exampleService , contentService );
126+ log .info ("结束" +new Date (System .currentTimeMillis ()).toString ());
123127 }
124128 return ResultMapUtils .ResultMap (bol , 0 , example .getExampleId ());
125129 } catch (Exception e ) {
@@ -154,6 +158,31 @@ public Map<String,Object> addFavorites(Favorites favorites){
154158 }
155159 }
156160
161+ /**
162+ * 取消喜爱
163+ * @param favorites
164+ * @return
165+ */
166+ @ PostMapping ("/cancelFavorites" )
167+ @ ApiOperation ("取消喜爱" )
168+ public Map <String ,Object > cancelFavorites (Favorites favorites ){
169+ if (StringUtils .isNotBlank (favorites .getUsername ())
170+ && StringUtils .isNotBlank (favorites .getExampleId ())){
171+ //取消喜爱
172+ Boolean bol = favoritesService .cancelFavorites (favorites );
173+ //清除缓存中的喜爱
174+ if (bol ){
175+ redisTemplate .opsForList ().remove (favorites .getUsername ()+"fav" , 0 , favorites .getExampleId ());
176+ }
177+ //更新用户喜爱数量
178+ Boolean aBoolean = accountService .reduceFavorites (favorites .getUsername ());
179+ log .info (aBoolean .toString ());
180+ return ResultMapUtils .ResultMap (bol ,0 ,null );
181+ }else {
182+ return ResultMapUtils .ResultMap (false ,1 ,null );
183+ }
184+ }
185+
157186
158187 /**
159188 * 放入回收站
@@ -162,7 +191,12 @@ public Map<String,Object> addFavorites(Favorites favorites){
162191 */
163192 @ DeleteMapping ("/" )
164193 @ ApiOperation ("放入回收站" )
165- public Map <String ,Object > deleteExample (Example example ){
194+ public Map <String ,Object > deleteExample (HttpServletRequest request , Example example ){
195+ //获取token中的用户名
196+ String username = JWTUtils .verify (request .getHeader ("token" ))
197+ .getClaim ("username" ).asString ();
198+ //存入用户名进行查询
199+ example .setUsername (username );
166200 //查询是否存在该实例
167201 Example query = exampleService .queryByIdUsername (example );
168202 if (query !=null ){
@@ -194,12 +228,29 @@ public Message postProcessMessage(Message message) throws AmqpException {
194228
195229 }
196230
231+ /**
232+ * 恢复实例
233+ * @return
234+ */
235+ @ PostMapping ("/resume" )
236+ public Map <String ,Object > resume (Example example ){
237+ example .setDeleted (0 );
238+ Boolean update = exampleService .update (example );
239+ if (update ){
240+ //减少回收站数量(恢复实例)
241+ accountService .reduceRecycle (example .getUsername ());
242+ //增加作品数
243+ accountService .addWorks (example .getUsername ());
244+ }
245+ return ResultMapUtils .ResultMap (update ,0 ,null );
246+ }
247+
197248 /**
198249 * 立即删除回收站
199250 * @param example
200251 * @return
201252 */
202- @ DeleteMapping ("/example " )
253+ @ DeleteMapping ("/delete " )
203254 @ ApiOperation ("立即删除回收站" )
204255 public Map <String ,Object > deleteRightNow (Example example ){
205256 //立即删除实例
@@ -220,5 +271,25 @@ public Map<String,Object> deleteRightNow(Example example){
220271 return ResultMapUtils .ResultMap (bol ,0 ,null );
221272 }
222273
274+ /**
275+ * 更新实例
276+ * @param example
277+ * @return
278+ */
279+ @ ApiOperation ("更新实例" )
280+ @ PutMapping ("/" )
281+ public Map <String ,Object > updateExample (HttpServletRequest request ,Example example ){
282+ //获取token中的用户名
283+ String username = JWTUtils .verify (request .getHeader ("token" ))
284+ .getClaim ("username" ).asString ();
285+ //存入用户名进行查询
286+ example .setUsername (username );
287+ Example query = exampleService .queryByIdUsername (example );
288+ Boolean update =false ;
289+ if (query !=null ){
290+ update = exampleService .update (example );
291+ }
292+ return ResultMapUtils .ResultMap (update ,0 ,null );
293+ }
223294
224295}
0 commit comments