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); } } }
/// <summary> /// Create a Photo entity of non-album. /// </summary> /// <param name="photoDTO"></param> /// <returns>-1 if user not found</returns> public static int AddPhoto(PhotoDTO photoDTO) { //get user User user; using (var db = new CF_FamsamEntities()) { user = db.User.Find(photoDTO.AuthorId); if (user == null) { return -1; } //create photo and post DateTime lastUpdate = DateTime.Now; Photo photo = new Photo(); photo.id = lastUpdate.Millisecond; photo.url = photoDTO.ImageURL; photo.badQuality = photoDTO.BadQuality; GeneralPost post = new GeneralPost(); post.Id = photo.id; post.postType = GeneralPost.PHOTO_POST_TYPE; post.Photo = photo; post.createUserId = user.id; post.CreateUser = user; post.lastUpdate = lastUpdate; foreach (string tagname in photoDTO.tags) { Tag tag = db.Tag.Find(tagname); if (tag == null) { tag = new Tag(); tag.name = tagname; db.Tag.Add(tag); } post.Tag.Add(tag); } try { db.GeneralPost.Add(post); } catch (Exception ex) { Debug.WriteLine("Exception on create photo: " + ex.ToString()); } return 1; } }