/// <summary> /// Authorization request header from client. /// </summary> /// <param name="header">header from client</param> /// <returns>-401/-403/{userId}</returns> public static long Authentication(HttpRequestHeaders header) { string authorization = header.GetValues("Authorization").FirstOrDefault(); if (authorization == null) { return -401; } using (var db = new CF_FamsamEntities()) { string token = authorization.Split(null)[1]; Session session = db.Session.Find(token); Debug.WriteLine("____________________________" + session.token); if (session == null) return -403; if (session.expired < DateTime.Now) { Debug.WriteLine("____________________________ session mili:" + session.expired.Millisecond); Debug.WriteLine("____________________________ now mili:" + DateTime.Now.Millisecond); //session expired db.Session.Remove(session); db.SaveChanges(); return -403; } else { return session.User.id; } } }
protected void InitializeDB() { using (CF_FamsamEntities context = new CF_FamsamEntities()) { //create user role UserRole userRole = context.UserRole.Find(UserRole.LOGGED_IN_ROLE); if ( userRole == null) { userRole = new UserRole(); userRole.rolename = UserRole.LOGGED_IN_ROLE; context.UserRole.Add(userRole); } //create user User createUser = (from u in context.User where u.email == "mrbean" select u).FirstOrDefault<User>(); if (createUser == null) { createUser = new User { id = DateTime.Now.Millisecond, email = "*****@*****.**", password = "******", firstname = "Lup", lastname = "Bean", UserRole = userRole, role = userRole.rolename }; context.User.Add(createUser); } //create session string token = Base64Utils.Base64Encode("mrbean:mrbean"); Session session = context.Session.Find(token); if (session == null) { session = new Session { token = token, expired = new DateTime(2100, 1, 1), User = createUser, }; context.Session.Add(session); } //new post for photo DateTime thisTime = DateTime.Now; GeneralPost post = new GeneralPost(); post.Id = thisTime.Millisecond; post.lastUpdate = thisTime; post.description = "haha"; post.CreateUser = createUser; post.createUserId = createUser.id; post.postType = GeneralPost.PHOTO_POST_TYPE; context.GeneralPost.Add(post); Photo photo = new Photo(); photo.Post = post; photo.url = "http://photo.url/nothing.jpg"; context.Photo.Add(photo); try { context.SaveChanges(); } catch (Exception ex) { Debug.WriteLine("Exception on Initialize DB Sample: " + ex); } } }
public static long Authentication(String token){ using (var db = new CF_FamsamEntities()) { Session session = db.Session.Find(token); if (session == null) return -403; if (session.expired < DateTime.Now) { Debug.WriteLine("____________________________ session mili:" + session.expired.Millisecond); Debug.WriteLine("____________________________ now mili:" + DateTime.Now.Millisecond); //session expired db.Session.Remove(session); db.SaveChanges(); return -403; } else { return session.User.id; } } }
// current user remove an album // return -1 if fail // return 0 if success public static int RemoveAlbum(long albumId, bool agreeToRemove) { using (var context = new CF_FamsamEntities()) { var album = context.Album.FirstOrDefault(a => a.id == albumId); if (agreeToRemove) { try { var listPhoto = album.Photo; album.Photo.Clear(); foreach(var photo in listPhoto) { context.Photo.Remove(photo); context.GeneralPost.Remove(photo.Post); } context.Album.Remove(album); context.GeneralPost.Remove(album.Post); context.SaveChanges(); } catch (Exception ex) { Debug.WriteLine("Exception: " + ex.StackTrace); return -1; } } else { try { album.Photo.Clear(); context.Album.Remove(album); context.GeneralPost.Remove(album.Post); context.SaveChanges(); } catch (Exception ex) { Debug.WriteLine("Exception: " + ex.StackTrace); return -1; } } return 0; } }
// current user update album title // return -1 if fail // return 0 if success public static int EditAlbum(AlbumDTO albumEdit, List<PhotoDTO> listPhotoAdd, List<PhotoDTO> listPhotoRemove) { using (var context = new CF_FamsamEntities()) { var post = context.GeneralPost.FirstOrDefault(p => p.Id == albumEdit.Id); var album = post.Album; post.lastUpdate = DateTime.Now; album.title = albumEdit.Title; post.description = albumEdit.Description; try { // add new list of photos to album if (listPhotoAdd.Count > 0) { foreach (var newPhoto in listPhotoAdd) { album.Photo.Add(context.Photo.FirstOrDefault(p => p.id == newPhoto.Id)); } } // remove list of photos from album if (listPhotoRemove.Count > 0) { foreach (var removePhoto in listPhotoRemove) { album.Photo.Remove(context.Photo.FirstOrDefault(p => p.id == removePhoto.Id)); } } context.Entry<Album>(album).State = EntityState.Modified; context.Entry<GeneralPost>(post).State = EntityState.Modified; context.SaveChanges(); } catch (Exception ex) { Debug.WriteLine("Exception: " + ex.StackTrace); return -1; } return 0; } }
// current user update album title // return -1 if fail // return 0 if success public static int EditAlbumTitle(AlbumDTO albumEdit) { using (var context = new CF_FamsamEntities()) { var post = context.GeneralPost.FirstOrDefault(p => p.Id == albumEdit.Id); post.lastUpdate = DateTime.Now; var album = context.Album.FirstOrDefault(a => a.id == albumEdit.Id); album.title = albumEdit.Title; try { context.Entry<Album>(album).State = EntityState.Modified; context.Entry<GeneralPost>(post).State = EntityState.Modified; context.SaveChanges(); } catch (Exception ex) { Debug.WriteLine("Exception: " + ex.StackTrace); return -1; } return 0; } }
// current user create new album // return -1 if fail // return 0 if success public static int CreateAlbum(AlbumDTO albumNew) { using (var context = new CF_FamsamEntities()) { if (albumNew == null) return -1; User user = context.User.FirstOrDefault(u => u.email.Equals(albumNew.AuthorEmail)); if (user == null) return -1; GeneralPost post = new GeneralPost(); post.Id = DateTime.Now.Millisecond; post.description = albumNew.Description; post.lastUpdate = DateTime.Now; post.createUserId = user.id; Album album = new Album(); album.id = post.Id; album.title = albumNew.Title; try { foreach (var photo in albumNew.ListPhoto) { album.Photo.Add(context.Photo.FirstOrDefault(p => p.id == photo.Id)); } context.GeneralPost.Add(post); context.Album.Add(album); context.SaveChanges(); } catch (Exception ex) { Debug.WriteLine("Exception: " + ex.StackTrace); return -1; } return 0; } }
/// <summary> /// Update photo /// </summary> /// <param name="photo"></param> /// <returns></returns> public static int EditDescription(PhotoDTO photoDTO) { using (var db = new CF_FamsamEntities()) { Photo photo = db.Photo.Find(photoDTO.Id); if (photo == null) return -1; //update photo DateTime lastUpdate = DateTime.Now; photo.Post.lastUpdate = lastUpdate; photo.Post.description = photoDTO.Description; try { db.Entry(photo).State = EntityState.Modified; db.SaveChanges(); return 1; } catch (Exception ex) { Debug.WriteLine("Exception on Edit photo description:" + ex.ToString()); } } return 0; }